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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top