G
Greg Maxey
For the last four days I have been engrossed in trying to build a macro to
find and highlight text. It has little practical use other than provide a
learning opportunity. I want to thank Steve Hudson, Jezebel (would be nice
to know what variety of person you are), Jason Eacott, and Dennis A for
their gracious assistance. Working on this macro I was exposed to Dos,
boleans, arrays, Private, ubounds, learned a little about With Selection,
and several other tips. Here is the result for anyone interested. Go ahead
and tear it apart. I always welcome contructive criticism and free
schooling:
Option Explicit
Private fHlight As Boolean
Private mFword As String
Private pCcnt As Long 'sets the highlight color
Sub CountOccurrences()
Dim pCount As Long 'The occurence counter
Dim pHlight As VbMsgBoxResult 'option to highlight
Dim pCycleCols() As Variant 'array of highlight colors
'Clears find and replace dialog
Call ResetFRParameters
'Define the array of highlight colors
pCycleCols = Array(wdYellow, wdBrightGreen, wdTurquoise, wdTeal, wdPink,
wdRed)
'This next bit controls the highlight color to use
'and keeps it within the array range
On Error Resume Next
pCcnt = ActiveDocument.Variables("pCcnt").Value
On Error GoTo 0
If pCcnt < 0 Or pCcnt > UBound(pCycleCols) Then
pCcnt = 0
End If
'User input and options. Set/Reset counter to 0
mFword = InputBox("Type in the word or phrase that you want to find and
count.")
If mFword = "" Then
Exit Sub 'exist on no input or user cancel
End If
pCount = 0
pHlight = MsgBox("Do you want to highlight each occurrence?", vbYesNo,
"Highlight")
With Selection
.HomeKey wdStory
.Find.ClearFormatting
With .Find
.Text = mFword
.Wrap = wdFindStop
.MatchWholeWord = True
.MatchCase = False
.Forward = True
.MatchAllWordForms = False
Do While .Execute = True
If pHlight = vbYes Then
With Selection
.Range.HighlightColorIndex = pCycleCols(pCcnt)
.Collapse wdCollapseEnd
End With
End If
pCount = pCount + 1 'counts each occurrrence of the word
Loop
End With
End With
pCcnt = pCcnt + 1 'Up sequences color array
ActiveDocument.Variables("pCcnt").Value = pCcnt 'stores the value
'Display results
If pCount > 1 Then
MsgBox Chr$(34) & mFword & Chr$(34) & " was found " & _
pCount & " times."
ElseIf pCount = 1 Then
MsgBox Chr$(34) & mFword & Chr$(34) & " was found " & _
pCount & " time."
Else
MsgBox Chr$(34) & mFword & Chr$(34) & " was not found."
End If
ActiveDocument.Variables("mFword").Value = mFword
End Sub
Sub ClearHL()
Dim pQuerry As VbMsgBoxResult
Dim pClearAll As VbMsgBoxResult
Call TestForAnyHighlight 'Check if anything is highlighted
If fHlight Then
'Clears all highlighting
pClearAll = MsgBox("Do you want to remove highlighting throughout the
document?", _
vbYesNoCancel, "Restore All")
If pClearAll = vbYes Then
With Selection
.WholeStory
.Range.HighlightColorIndex = wdNoHighlight
.Collapse Direction:=wdCollapseStart
ActiveDocument.Variables("pCcnt").Value = 0
End With
Exit Sub
ElseIf pClearAll = vbCancel Then
Exit Sub
End If
'Gets last word highlighted
On Error Resume Next
mFword = ActiveDocument.Variables("mFword").Value
On Error GoTo 0
'Clears highlighting from last word
If Len(mFword) > 0 Then
If MsgBox("Do you want to restore normal formatting to " & Chr$(34) &
mFword & Chr$(34) & " the last word highlighted?", _
vbYesNo, "Restore Last") = vbYes Then
Call RemoveHighlight(mFword, pCcnt)
'Option to clear more if exists
Call TestForAnyHighlight
If fHlight Then
pQuerry = MsgBox("Do you want to restore normal formatting to another
individual phrase or character?", _
vbYesNo, "Continue")
End If
mFword = ""
End If
End If
If Len(mFword) = 0 Or pQuerry = vbYes Then
Do
mFword = InputBox$("Type in the word or phrase that you want restore
normal formatting.")
If LenB(mFword) = 0 Then
Exit Do
End If
Call RemoveHighlight(mFword, pCcnt)
Call TestForAnyHighlight
If fHlight Then
pQuerry = MsgBox("Do you want to restore normal formatting an
another individual phrase or character?", _
vbYesNoCancel, "Continue")
Else: Exit Do
End If
Loop Until pQuerry > 6
End If
Else: MsgBox "There is no higlighted text in the document"
End If
End Sub
Sub ResetFRParameters()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Sub
Sub RemoveHighlight(mFword As String, pCcnt As Long)
With Selection
.HomeKey wdStory
.Find.ClearFormatting
Do While .Find.Execute(FindText:=mFword, Wrap:=wdFindStop, _
MatchWholeWord:=True, Forward:=True) = True
Selection.Range.HighlightColorIndex = wdNoHighlight
Selection.Collapse wdCollapseEnd
Loop
End With
pCcnt = pCcnt - 1
ActiveDocument.Variables("pCcnt").Value = pCcnt
End Sub
Sub TestForAnyHighlight()
Call ResetFRParameters
fHlight = False
With Selection
.HomeKey wdStory
.Find.ClearFormatting
.Find.Highlight = True
If .Find.Execute Then
.Collapse wdCollapseEnd
fHlight = True
End If
End With
End Sub
find and highlight text. It has little practical use other than provide a
learning opportunity. I want to thank Steve Hudson, Jezebel (would be nice
to know what variety of person you are), Jason Eacott, and Dennis A for
their gracious assistance. Working on this macro I was exposed to Dos,
boleans, arrays, Private, ubounds, learned a little about With Selection,
and several other tips. Here is the result for anyone interested. Go ahead
and tear it apart. I always welcome contructive criticism and free
schooling:
Option Explicit
Private fHlight As Boolean
Private mFword As String
Private pCcnt As Long 'sets the highlight color
Sub CountOccurrences()
Dim pCount As Long 'The occurence counter
Dim pHlight As VbMsgBoxResult 'option to highlight
Dim pCycleCols() As Variant 'array of highlight colors
'Clears find and replace dialog
Call ResetFRParameters
'Define the array of highlight colors
pCycleCols = Array(wdYellow, wdBrightGreen, wdTurquoise, wdTeal, wdPink,
wdRed)
'This next bit controls the highlight color to use
'and keeps it within the array range
On Error Resume Next
pCcnt = ActiveDocument.Variables("pCcnt").Value
On Error GoTo 0
If pCcnt < 0 Or pCcnt > UBound(pCycleCols) Then
pCcnt = 0
End If
'User input and options. Set/Reset counter to 0
mFword = InputBox("Type in the word or phrase that you want to find and
count.")
If mFword = "" Then
Exit Sub 'exist on no input or user cancel
End If
pCount = 0
pHlight = MsgBox("Do you want to highlight each occurrence?", vbYesNo,
"Highlight")
With Selection
.HomeKey wdStory
.Find.ClearFormatting
With .Find
.Text = mFword
.Wrap = wdFindStop
.MatchWholeWord = True
.MatchCase = False
.Forward = True
.MatchAllWordForms = False
Do While .Execute = True
If pHlight = vbYes Then
With Selection
.Range.HighlightColorIndex = pCycleCols(pCcnt)
.Collapse wdCollapseEnd
End With
End If
pCount = pCount + 1 'counts each occurrrence of the word
Loop
End With
End With
pCcnt = pCcnt + 1 'Up sequences color array
ActiveDocument.Variables("pCcnt").Value = pCcnt 'stores the value
'Display results
If pCount > 1 Then
MsgBox Chr$(34) & mFword & Chr$(34) & " was found " & _
pCount & " times."
ElseIf pCount = 1 Then
MsgBox Chr$(34) & mFword & Chr$(34) & " was found " & _
pCount & " time."
Else
MsgBox Chr$(34) & mFword & Chr$(34) & " was not found."
End If
ActiveDocument.Variables("mFword").Value = mFword
End Sub
Sub ClearHL()
Dim pQuerry As VbMsgBoxResult
Dim pClearAll As VbMsgBoxResult
Call TestForAnyHighlight 'Check if anything is highlighted
If fHlight Then
'Clears all highlighting
pClearAll = MsgBox("Do you want to remove highlighting throughout the
document?", _
vbYesNoCancel, "Restore All")
If pClearAll = vbYes Then
With Selection
.WholeStory
.Range.HighlightColorIndex = wdNoHighlight
.Collapse Direction:=wdCollapseStart
ActiveDocument.Variables("pCcnt").Value = 0
End With
Exit Sub
ElseIf pClearAll = vbCancel Then
Exit Sub
End If
'Gets last word highlighted
On Error Resume Next
mFword = ActiveDocument.Variables("mFword").Value
On Error GoTo 0
'Clears highlighting from last word
If Len(mFword) > 0 Then
If MsgBox("Do you want to restore normal formatting to " & Chr$(34) &
mFword & Chr$(34) & " the last word highlighted?", _
vbYesNo, "Restore Last") = vbYes Then
Call RemoveHighlight(mFword, pCcnt)
'Option to clear more if exists
Call TestForAnyHighlight
If fHlight Then
pQuerry = MsgBox("Do you want to restore normal formatting to another
individual phrase or character?", _
vbYesNo, "Continue")
End If
mFword = ""
End If
End If
If Len(mFword) = 0 Or pQuerry = vbYes Then
Do
mFword = InputBox$("Type in the word or phrase that you want restore
normal formatting.")
If LenB(mFword) = 0 Then
Exit Do
End If
Call RemoveHighlight(mFword, pCcnt)
Call TestForAnyHighlight
If fHlight Then
pQuerry = MsgBox("Do you want to restore normal formatting an
another individual phrase or character?", _
vbYesNoCancel, "Continue")
Else: Exit Do
End If
Loop Until pQuerry > 6
End If
Else: MsgBox "There is no higlighted text in the document"
End If
End Sub
Sub ResetFRParameters()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Sub
Sub RemoveHighlight(mFword As String, pCcnt As Long)
With Selection
.HomeKey wdStory
.Find.ClearFormatting
Do While .Find.Execute(FindText:=mFword, Wrap:=wdFindStop, _
MatchWholeWord:=True, Forward:=True) = True
Selection.Range.HighlightColorIndex = wdNoHighlight
Selection.Collapse wdCollapseEnd
Loop
End With
pCcnt = pCcnt - 1
ActiveDocument.Variables("pCcnt").Value = pCcnt
End Sub
Sub TestForAnyHighlight()
Call ResetFRParameters
fHlight = False
With Selection
.HomeKey wdStory
.Find.ClearFormatting
.Find.Highlight = True
If .Find.Execute Then
.Collapse wdCollapseEnd
fHlight = True
End If
End With
End Sub