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

calendar_today Asked Oct 25, 2010
thumb_up 7 upvotes
history Updated April 16, 2026

Question posted 2010 · +9 upvotes

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.

Accepted answer +7 upvotes

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);
} 

Word VBA objects referenced (5)

Top ms-word Q&A (6)

+7 upvotes ranks this answer #16 out of 31 ms-word solutions on this site .