MialLewis
09-28-2017, 07:37 AM
Hi all,
Hoping you can solve my problem... I've spent hours looking for a solution and still no luck.
I'm using the latest version of word via Office 365.
I am writing a program that will search through a document using a list of words (I haven't put this feature in yet, its just searching for a static term) and will highlight the first occurrence one colour, and the subsequent occurrences another. The code fulfills this function, but won't end as it is stuck in a while loop. I have no idea why, the logic is find to my understanding. Can anyone shed any light upon why this is happening?
Thanks in advance, code is below.
Sub Highlight()
'Application.ScreenUpdating = False 'Turn off updating for speed
Set SearchRange = ActiveDocument.Content 'Set Range to search
Options.DefaultHighlightColorIndex = wdYellow
Dim A As String 'Introduce variable for Words
'Loop will start here (not introduced yet)
With SearchRange.Find
.Text = "TEST"
.Replacement.Text = "TEST"
.Replacement.Highlight = True 'Highlight
.MatchWholeWord = True
.MatchCase = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = True
.Wrap = wdFindEnd
.MatchWildcards = False
Do While .Execute(Replace:=wdReplaceOne)
Options.DefaultHighlightColorIndex = wdTeal
Loop
End With
Options.DefaultHighlightColorIndex = wdYellow
'Loop will end here (not introduced yet)
'Application.ScreenUpdating = True
End Sub
Hoping you can solve my problem... I've spent hours looking for a solution and still no luck.
I'm using the latest version of word via Office 365.
I am writing a program that will search through a document using a list of words (I haven't put this feature in yet, its just searching for a static term) and will highlight the first occurrence one colour, and the subsequent occurrences another. The code fulfills this function, but won't end as it is stuck in a while loop. I have no idea why, the logic is find to my understanding. Can anyone shed any light upon why this is happening?
Thanks in advance, code is below.
Sub Highlight()
'Application.ScreenUpdating = False 'Turn off updating for speed
Set SearchRange = ActiveDocument.Content 'Set Range to search
Options.DefaultHighlightColorIndex = wdYellow
Dim A As String 'Introduce variable for Words
'Loop will start here (not introduced yet)
With SearchRange.Find
.Text = "TEST"
.Replacement.Text = "TEST"
.Replacement.Highlight = True 'Highlight
.MatchWholeWord = True
.MatchCase = True
.ClearFormatting
.MatchWholeWord = True
.MatchCase = True
.Wrap = wdFindEnd
.MatchWildcards = False
Do While .Execute(Replace:=wdReplaceOne)
Options.DefaultHighlightColorIndex = wdTeal
Loop
End With
Options.DefaultHighlightColorIndex = wdYellow
'Loop will end here (not introduced yet)
'Application.ScreenUpdating = True
End Sub