Difficulty with finding a string in MS Word table using Interop - searchArea.Find.Execute()

Franta Dvojka 6 Reputation points
2021-08-20T15:54:29.897+00:00

I am having difficulty with finding all occurrences of a magic string in MS Word doc using Interop - Word.Range - searchArea.Find.Execute().

*// ***** Basic concept
Word.Range searchArea = Application.ActiveDocument;
...
searchArea.Find.Execute();
if (searchArea.Find.Found) then
searchArea.Start = narrow the search area as needed
searchArea.End = end of the document
...
search next occurrence
// ***** End of story*

When the magic word is located and found in a table, the next search recognizes the occurrence already found as if searching to the left.

It looks like this anomalous behavior occurs if the searchArea.Start index is anywhere between the end of the (previous) magic word and the end of the table row.

When I move the searchArea.Start index one row down everything gets back to normal.

How can I safely search Forward within MS Word table ?

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,248 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2021-08-24T08:26:20.597+00:00

    @Franta Dvojka , based on my test, I find the startindex indeed exists some problems. I used the following code to make a test.

    static void Main(string[] args)  
            {  
                object missing = Type.Missing;  
                Word.Application app = new Word.Application();  
                Word.Document document = app.Documents.Open("D:\\3.docx");  
                //app.Visible = true;  
                Word.Range rng = document.Content;  
                rng.Start =80;  
                rng.End = 94;  
                rng.Find.ClearFormatting();  
                rng.Find.Forward = false ;  
                rng.Find.Text = "Name";  
      
                rng.Find.Execute(  
           ref missing, ref missing, ref missing, ref missing, ref missing,  
           ref missing, ref missing, ref missing, ref missing, ref missing,  
           ref missing, ref missing, ref missing, ref missing, ref missing);  
                int intFound = 0;  
                 
      
                while (rng.Find.Found)  
                {  
                    intFound++;  
                    rng.Find.Execute(  
           ref missing, ref missing, ref missing, ref missing, ref missing,  
           ref missing, ref missing, ref missing, ref missing, ref missing,  
           ref missing, ref missing, ref missing, ref missing, ref missing);  
      
                }  
                document.Close();  
                Console.WriteLine(intFound);  
                Console.ReadKey();  
                  
                  
            }  
    

    The word example:

    125962-1.png

    I can searched all the text called "Name" 3 times, but I set the index from 80~94. I think there is some problem here.

    Therefore, I have two suggestions about your problem.

    First, please try to avoid searching the text by using start index and end index in Table and use it in paragraph.

    Second, you can try my code to check if the searching to the left.


    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.