Why does this code only catch the first occurrence?

F

Frank Lane

Dim docStory As Range
Set docStory = ActiveDocument.Content

With docStory.Find
.ClearFormatting
.Execute FindText:="Acronym List",Forward:=True, Wrap:=wdFindContinue
If .Found Then
docStory.HighlightColorIndex = wdTurquoise
Debug.Print "found one"
End If
End With
 
J

Jay Freedman

Dim docStory As Range
Set docStory = ActiveDocument.Content

With docStory.Find
.ClearFormatting
.Execute FindText:="Acronym List",Forward:=True, Wrap:=wdFindContinue
If .Found Then
docStory.HighlightColorIndex = wdTurquoise
Debug.Print "found one"
End If
End With

It only finds the first one because you only told it to run .Execute once. If you
want it to find all occurrences in the body of the document, use this instead.

Dim docStory As Range
Set docStory = ActiveDocument.Content

With docStory.Find
.ClearFormatting
While .Execute(FindText:="Acronym List", Forward:=True, Wrap:=wdFindStop)
If .Found Then
docStory.HighlightColorIndex = wdTurquoise
Debug.Print "found one"
End If
Wend
End With
 
Top