Find.Execute with Find.Format = True keeps looping

H

Hilmar Everts

I have a Word XP document with, for example, 3 words that are highlighted
brightgreen and 1 word that is highligted red. With the code below I want to
remove the brightgreen highlight and give the red highlight
DoubleStrikeThrough and Outline. Unfortunately the code keeps looping.
Somehow the formatting (highlight) triggers this because when I do something
similar with a normal text search, everything goes peachy. Can anyone help
fix this problem ?

Sub FindWithFormat()
Dim oStoryRange As Range
Dim oResult As Range

Set oStoryRange = ActiveDocument.StoryRanges(wdMainTextStory)
Do
Set oResult = oStoryRange.Duplicate
With oResult.Find
.ClearFormatting
.Replacement.ClearFormatting
.Highlight = True
.Font.StrikeThrough = False
.Font.DoubleStrikeThrough = False
.Font.Outline = False
.Forward = True
.Wrap = wdFindStop
If .Execute Then
If oResult.HighlightColorIndex = wdUndefined Or
wdNoHighlight Then
'\\ Do nothing
ElseIf oResult.HighlightColorIndex = wdBrightGreen Then
oResult.HighlightColorIndex = wdNoHighlight
Else
oResult.Font.DoubleStrikeThrough = True
oResult.Font.Outline = True
End If
End If
End With
Loop While oResult.Find.Found

End Sub
 
C

Chuck

Tried your code, works for me in Word 2000, not sure if/why it would be
different in Word XP. Is there something funky with the document or does it
loop endlessly on any document you test?

Just curious, why use oResult for your search (ie why work on a duplicate of
oStoryRange instead of oStoryRange itself)? I tried the code replacing
oResult with oStoryRange and it worked just as well...
 
H

Hilmar Everts

Chuck said:
Tried your code, works for me in Word 2000, not sure if/why it would be
different in Word XP. Is there something funky with the document or does it
loop endlessly on any document you test?

All the documents on my Win2K-SP4 OffXP machine loop endlessly :-(
Just curious, why use oResult for your search (ie why work on a duplicate of
oStoryRange instead of oStoryRange itself)? I tried the code replacing
oResult with oStoryRange and it worked just as well...

That's because it's part of a larger piece of code, but for purpose sake I
only copied the code necessary to state my question. And because a succesful
find redefines the range object so i figured that on the next .execute the
search wouldn't find anything because the search range would be the previous
result range.
 
H

Hilmar Everts

Hilmar Everts said:
All the documents on my Win2K-SP4 OffXP machine loop endlessly :-(

I was wrong on this one. I only tested on similar documents.
I took one of the problem documents and deleted some of the contents to make
the document smaller. And guess what. The problem is gone in that smaller
document.

However I have no idea why the smaller document doesn't loop endlessly :-(
And I don't know how to post my testdocs in this forum.
 
G

Greg

How about:

Sub HilmarsFindWithFormat()
Dim myRange As Range
Set myRange = ActiveDocument.StoryRanges(wdMainTextStory)
With myRange.Find
.ClearFormatting
.Replacement.ClearFormatting
.Highlight = True
.Forward = True
.Wrap = wdFindStop
Do While .Execute
If myRange.HighlightColorIndex = wdBrightGreen Then
myRange.HighlightColorIndex = wdNoHighlight
ElseIf myRange.HighlightColorIndex = wdRed Then
myRange.Font.DoubleStrikeThrough = True
End If
myRange.Collapse Direction:=wdCollapseEnd
Loop
End With
End Sub
 
H

Hilmar Everts

Nope, also no luck with this one.

I can't understand why smaller documents don't have the problem.
 

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