
November 21st, 2003, 07:20 AM
|
 |
Junior Member
|
|
Join Date: Oct 2003
Posts: 16
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
I have this code working which highlights the whole line, but I just want to highlight the word.
How do I modify it to just select the dirty word?
Code:
Sub Highlighter()
'
' Highlighter Macro
' Macro created 11/21/2003
'
Dim objDoc As Word.Document
Dim lNumberOfLines As Long
Dim lCount As Long
Dim sDirtyWords As Variant
Dim sWord As Variant
Dim HighlighColor As Variant
' Get the ActiveDocument
Set objDoc = ActiveDocument
' Set the highlight color - could be wdOrange, wdBlue...
HighlighColor = wdYellow
' Load words into array
sDirtyWords = Array("and", "for", "the", "which")
' Get number of lines in document
lNumberOfLines = objDoc.ComputeStatistics(wdStatisticLines)
' Remove all highlighting in document
Selection.WholeStory
Selection.Range.HighlightColorIndex = wdNoHighlight
' Loop through all the lines in the document
For lCount = 1 To lNumberOfLines
' Go to line
SelectLine lCount
' Loop through all words
For Each sWord In sDirtyWords
If InStr(1, Selection.Text, sWord) > 0 Then
Selection.Range.HighlightColorIndex = wdYellow
End If
Next
Next
' Remove handle to document
Set objDoc = Nothing
End Sub
Sub SelectLine(lLine As Long)
Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst, Count:=lLine, Name:=""
Selection.Find.ClearFormatting
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
' Select line
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
End Sub
|