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)
ActiveDocument.Content— Create an HTML File with a Table of Contents based on Cell DataActiveDocument.Content— Add a Table of Contents to a WorkbookRange— Refer to Cells by Using a Range ObjectRange— Delete Duplicate Entries in a RangeWord.Find— Find All the Sparklines on a Sheet
Top ms-word Q&A (6)
- XML – adding new line +19 (2012)
- How to open and manipulate Word document/template in Java? +18 (2012)
- Why does the file utility identify Microsoft Word files as CDF? What is this CDF? +15 (2011)
- Version Control for word documents +13 (2008)
- programatically convert word docx to doc without using ole automation +13 (2008)
- What makes Microsoft-Word-generated HTML documents so large in code? +12 (2015)
ms-word solutions on this site
.