question

FrantaDvojka-6984 avatar image
0 Votes"
FrantaDvojka-6984 asked FrantaDvojka-6984 commented

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

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 ?




dotnet-csharp
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@FrantaDvojka-6984 , could you provide the related code about your searchArea.Start index? Then, I also want to know how do you detect every search by using the Find.Execute method.


0 Votes 0 ·

See the update in the body of my initial question.

0 Votes 0 ·

1 Answer

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered FrantaDvojka-6984 commented

@FrantaDvojka-6984, 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.





1.png (5.5 KiB)
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you so very much for your feedback.

-May I ask you to say explicitly whether the code you've published is fully operational in a Table or failing?
-I am not quite sure what you mean by "avoid .. in Table and use it in paragraph". May I ask you to elaborate on that?

Hardly can I believe that searching for a magic word through an entire Word doc might lead to a fundamental issue as this task is most likely successfully accomplished by tons of applications I would think.

If the problem is real, I am sure it had been already documented. Do you happen to have access to relevant Microsoft documentation?










0 Votes 0 ·

May I ask you to clearly mark your code as Problem manifestation rather than solution?
A few comment lines at the top of your snippet will suffice.

I am a tad worried that your current contribution might mislead MVPs visiting this page in the future. Hope you understand.

0 Votes 0 ·

@FrantaDvojka-6984 . sorry for the late reply.

-May I ask you to say explicitly whether the code you've published is fully operational in a Table or failing?
-I am not quite sure what you mean by "avoid .. in Table and use it in paragraph". May I ask you to elaborate on that?

I means that the code can find the Name 3 times even if I set the set the index from 80~94.(All the doc is start from 0 and end with 94) Based on my test, It works well in a paragraph. But it is not precise for only the table.


Hardly can I believe that searching for a magic word through an entire Word doc might lead to a fundamental issue as this task is most likely successfully accomplished by tons of applications I would think.

If the problem is real, I am sure it had been already documented. Do you happen to have access to relevant Microsoft documentation?

Currently I can not make sure that it is a common issue or not. I have read the related doc word.range.start. The question up to here, I think the problem is not the c# problem. As usual, we can use c# to do some operations on the word by using interop API. But it comes from the VBA After all. Therefore, I recommend that you could provide feedback in
Office VBA support and feedback if you still have questions.

Thanks for your understanding.


0 Votes 0 ·

Thank you for your efforts.

0 Votes 0 ·