Interop.Word.Find - Infinite looping when found hyperlink

Zadumkin Lev 1 Reputation point
2021-04-01T12:42:01.493+00:00

I came across an infinite loop problem when I used Find.Execute() in text that contains a hyperlink: the parent Range/Select object, from which the Find object was obtained, stops shifting to the next match.

while (rng.Find.Found)
    {
        if (rng.Hyperlinks.Count > 0)
            test = rng.Start;
        if (rng.Text.Trim().Length > 0)
        {
            // do something 
        }
        rng.Find.Execute();
        if (rng.Start == test)
            MessageBox.Show("it will be infinite");
    }
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,177 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,452 questions
Word Management
Word Management
Word: A family of Microsoft word processing software products for creating web, email, and print documents.Management: The act or process of organizing, handling, directing or controlling something.
891 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-04-02T06:51:20.85+00:00

    Hi ZadumkinLev-1386,
    Please try to add the following code before the while statement loop:

    rng.Find.Execute();  
    while (rng.Find.Found)  
    {  
      ...  
    }  
    

    More details please refer to this document.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.