An implementation of Visual Basic that is built into Microsoft products.
Use a loop that repeatedly calls Find.Execute and stops when .Found is False. Also set Wrap = wdFindStop so the search ends at the end of the document instead of wrapping.
A pattern based on the documented approach with Range.Find is more reliable than using Selection directly. Example that loops all occurrences of "F90" to the end of the document:
Sub Word_to_Excel_Part_One()
Dim rng As Range
Set rng = ActiveDocument.Content
With rng.Find
.ClearFormatting
.Text = "F90"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop ' stop at end of document
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While rng.Find.Execute
' rng is now at the found "F90"; perform the rest of the logic here
' Example: insert paragraph before F90
rng.Collapse Direction:=wdCollapseStart
rng.MoveStart Unit:=wdCharacter, Count:=-1
rng.InsertParagraphBefore
' Now search for the next occurrence, starting after the current one
rng.Collapse Direction:=wdCollapseEnd
Loop
End Sub
Key points from the Word object model:
- Use
Range.Findwhen looping through multiple matches so the selection is not constantly changed and the range is redefined on each successful find. - Use
.Forward = Trueto search forward through the document. - Use
.Wrap = wdFindStopto stop at the end of the document instead of continuing from the beginning. - After each successful
Execute, collapse the range to its end (wdCollapseEnd) before the nextExecuteso the search continues after the last match.
The same pattern can be applied to the later Find calls for "kg" and the wildcard pattern; each should use a Range and a loop, with Wrap = wdFindStop, and collapse the range between iterations.
References: