K
Karen
Hi All,
I need some help with some code that sometimes works and sometimes enters an
infinite loop. Using the 'find' feature in Word, I want to find selections
highlighted in either red or turquoise. Depending on the color of the
highlight, I increment a counter either full value or half value. Sometimes
it works, sometimes it just keeps looping. I think the basic problem is
that I some way of find the end of the document to specify the range.
Here's the code:
Sub TestScoring()
Dim newcount As Single
'go to top of doc
Selection.HomeKey Unit:=wdStory
'clear previous find
Selection.Find.ClearFormatting
'set search for highlight on
Selection.Find.Highlight = True
'run search
Selection.Find.Execute
'while highlights are still found
While Selection.Find.Found = True
'if selection is red then count full value
If Selection.Range.HighlightColorIndex = wdRed Then
newcount = newcount + 1
'if selection is turquoise then count half value
ElseIf Selection.Range.HighlightColorIndex = wdTurquoise Then
newcount = newcount + 0.5
End If
'continue same search
Selection.Find.ClearFormatting
Selection.Find.Highlight = True
Selection.Find.Execute
'end loop when no more found
Wend
'move back to top of doc when done
Selection.HomeKey Unit:=wdStory
MsgBox (newcount)
End Sub
Any ideas?
Karen
I need some help with some code that sometimes works and sometimes enters an
infinite loop. Using the 'find' feature in Word, I want to find selections
highlighted in either red or turquoise. Depending on the color of the
highlight, I increment a counter either full value or half value. Sometimes
it works, sometimes it just keeps looping. I think the basic problem is
that I some way of find the end of the document to specify the range.
Here's the code:
Sub TestScoring()
Dim newcount As Single
'go to top of doc
Selection.HomeKey Unit:=wdStory
'clear previous find
Selection.Find.ClearFormatting
'set search for highlight on
Selection.Find.Highlight = True
'run search
Selection.Find.Execute
'while highlights are still found
While Selection.Find.Found = True
'if selection is red then count full value
If Selection.Range.HighlightColorIndex = wdRed Then
newcount = newcount + 1
'if selection is turquoise then count half value
ElseIf Selection.Range.HighlightColorIndex = wdTurquoise Then
newcount = newcount + 0.5
End If
'continue same search
Selection.Find.ClearFormatting
Selection.Find.Highlight = True
Selection.Find.Execute
'end loop when no more found
Wend
'move back to top of doc when done
Selection.HomeKey Unit:=wdStory
MsgBox (newcount)
End Sub
Any ideas?
Karen