Newbie - Help Please

m0dified
11-21-2003, 03:37 AM
Hi-

I would like to write a Word Macro that searches a document for a set of pre-defined words and highlights them.

Can someone help me out with this problem?

I am completely new to VBA, but I am a developer.

m0dified
11-21-2003, 06:23 AM
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?



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

Scarber
01-02-2004, 05:14 PM
This doesn't highlight, but it probably does what you want.

Sub HighLightWeasels()

Dim sDirtyWords As Variant
Dim sWord As Variant

' Load words into array
sDirtyWords = Array(" that ", " just ", " very ", " nearly ", _
" almost ", " really ", " seem ", " appear ", _
" felt ", " feel ", " begin ", " began ", _
" would ", " should ", " could ", " quite ", _
" few ", " rather ", " thing ", " stuff ", _
" anyway ", " because ", "ly", " so ", " then ", _
" even ", " only ", "sit down", "stand up", _
" got ", " get ", " it ", " is ", " am ", " are ", _
" was ", " were ", " has ", " had ", " have ", _
" been ", "to be", "there is", "there are", _
"there was", "there were")

' Loop through all words

For Each sWord In sDirtyWords
With ActiveDocument.Content.Find
.ClearFormatting
.Text = CStr(sWord)
With .Replacement
.ClearFormatting
.Text = CStr(sWord)
.Font.Underline = wdUnderlineThick
.Font.UnderlineColor = wdColorBlue
End With
.Execute Format:=True, Replace:=wdReplaceAll
End With
Next


End Sub

LarryHazard
01-05-2004, 10:19 PM
Sub HighLiter()
' Load words into array
sDirtyWords = Array("and", "for", "the", "which")

For Each sWord In sDirtyWords
Selection.HomeKey Unit:=wdStory
Options.DefaultHighlightColorIndex = wdYellow
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Text = "(<" & sWord & ">)"
.Replacement.Text = "\1"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Next
End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum