C#: Searching a text in Word and getting the range of the result

c#, find, ms-word

Solution

Have you tried this:

 range.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);


 while (range.Find.Found) 
{ 
   //Get selected index.
   // Do as you please with range...
   //Positions:  range.Start... range.End
   //search again
   range.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);
} 

Problem

I can find a text in a Word file via: ``` Word.Range range = wordApp.ActiveDocument.Content; Word.Find find = range.Find; find.Text = "xxx"; find.ClearFormatting(); 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); ``` This tells me if the text is found. But I need the range of the found text-piece.

Original source