Hi All,
I have a longish userform macro in Word that highlights defined terms in legal documents in different colors depending on if they are defined more than once (in quotes), defined but not used, and then every instance of the term.
This all works perfectly on the first run of a document, however, if I run it again on a 2nd document, it jumps out of one of the loops and goes to the next phase of the routine, ending prematurely.
If I close and restart Word, it runs perfectly again, but fails on 2nd attempt. It appears to fail when changing from the main story to the footnote story (highlighted in red) below. This is just the bit of code where it fails - please let me know if anyone would like to see the whole thing.
Has anyone had a similar issue? Many thanks for any help!
Code:With Selection
.HomeKey wdStory
'highlight all copies yellow
With .Find
.ClearFormatting
.Replacement.ClearFormatting
Do While .Execute(FindText:=StrFind2, _
MatchCase:=False, MatchWholeWord:=False, MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set oRng = Selection.Range
oRng.HighlightColorIndex = wdYellow
i = i + 1
ActiveDocument.UndoClear
Loop
If ActiveDocument.Footnotes.Count >= 1 Then
ActiveDocument.StoryRanges(wdFootnotesStory).Select
Do While .Execute(FindText:=StrFind2, _
MatchCase:=False, MatchWholeWord:=False, MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Set oRng = Selection.Range
oRng.HighlightColorIndex = wdYellow
i = i + 1
ActiveDocument.UndoClear
Loop
End If
ActiveDocument.StoryRanges(wdMainTextStory).Select