-
Gmaxey,
I am having trouble tracing the code you provided. I added a few terms to the array and have been stepping through the macro. It seems that when the first term is found and all of the instances of it have been replaced, the range is not redefined to include the entire document when looking for the next term. If the next term is not between where the range was collapsed and the end of the document, none of the remaining terms are found. How do you redefine the range to include the entire document again? All of the attempts I made in doing so caused an infinite loop. Please see my comments in the code below.
[VBA]Sub DemoX()
Dim oRng As Range, arrWords, i As Long, HiLite As Variant
Set oRng = ActiveDocument.Range
With oRng.Find
arrWords = Array("turkey", "and", "vulture", "pay", "an")
.ClearFormatting
.Replacement.ClearFormatting
.MatchWholeWord = False
.Replacement.Text = "^&"
.Replacement.Highlight = True
HiLite = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdYellow
For i = 0 To UBound(arrWords)
.Text = arrWords(i)
'Resetting the range below causeses an infinite loop
'Set oRng = ActiveDocument.Range
While .Execute(Replace:=wdReplaceOne)
oRng.Collapse wdCollapseEnd
MsgBox "You have used a restricted word: " & arrWords(i)
Wend
'Resetting the range below causeses an infinite loop here too. Setting a
' breakpoint here shows this line is never executed, yet an infinite loop
' still results. How does this happen?
'Set oRng = ActiveDocument.Range
Next
End With
Set oRng = Nothing
End Sub
[/VBA]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules