Problem with highlighting words

R

Raj

Hi,

I have a word document containing the following sentence.

All people in the city – big and small had arrived to watch the
marathon from start to finish

I am running the macro below (adapted from a post in this group) to
highlight the words in the vFindText array.

Sub HighlightWords()
Dim vFindText As Variant
Dim sReplText As String
Dim sHighlight As String
Dim i As Long
sHighlight = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdBrightGreen
vFindText = Array("big", "small", "start", "finish")
sReplText = ""
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = False
For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = sReplText
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
Next i
End With
Options.DefaultHighlightColorIndex = sHighlight
End Sub

The problem: Only the words "big" and "finish" are getting highlighted
in green. The words "small" and "start" are not. What could be
problem?

Thanks in advance for the help.

Regards,
Raj
 
R

Raj

Hi,

I have a word document containing the following sentence.

All people in the city – big and small had arrived to watch the
marathon from start to finish

I am running the macro below (adapted from a post in this group) to
highlight the words in the vFindText array.

Sub HighlightWords()
Dim vFindText As Variant
Dim sReplText As String
Dim sHighlight As String
Dim i As Long
sHighlight = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdBrightGreen
vFindText = Array("big", "small", "start", "finish")
sReplText = ""
With Selection.Find
   .Forward = True
   .Wrap = wdFindContinue
   .MatchWholeWord = True
   .MatchWildcards = False
   .MatchSoundsLike = False
   .MatchAllWordForms = False
   .Format = True
   .MatchCase = False
   For i = LBound(vFindText) To UBound(vFindText)
       .Text = vFindText(i)
       .Replacement.Text = sReplText
       .Replacement.Highlight = True
       .Execute Replace:=wdReplaceAll
   Next i
End With
Options.DefaultHighlightColorIndex = sHighlight
End Sub

The problem: Only the words "big" and "finish" are getting highlighted
in green. The words "small" and "start" are not. What could be
problem?

Thanks in advance for the help.

Regards,
Raj

I have something more to say about the problem:
I cleaned up the highlighting and ran the macro again. It now
highlights only the "big" . None of the other three words in the
sentence are highlighted.

Thanks and Regards,
Raj
 
J

Jean-Guy Marcil

Raj said:
Hi,

I have a word document containing the following sentence.

All people in the city – big and small had arrived to watch the
marathon from start to finish

I am running the macro below (adapted from a post in this group) to
highlight the words in the vFindText array.

Sub HighlightWords()
Dim vFindText As Variant
Dim sReplText As String
Dim sHighlight As String
Dim i As Long
sHighlight = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdBrightGreen
vFindText = Array("big", "small", "start", "finish")
sReplText = ""
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = False
For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = sReplText
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
Next i
End With
Options.DefaultHighlightColorIndex = sHighlight
End Sub

The problem: Only the words "big" and "finish" are getting highlighted
in green. The words "small" and "start" are not. What could be
problem?

Thanks in advance for the help.

Try this instead:

Dim vFindText As Variant
Dim sReplText As String
Dim sHighlight As String
Dim i As Long
Dim rngStart As Range
Dim rngFind As Range

Set rngStart = Selection.Range
Set rngFind = ActiveDocument.Range

sHighlight = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdBrightGreen
vFindText = Array("big", "small", "start", "finish")
sReplText = ""

With rngFind.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = False
For i = LBound(vFindText) To UBound(vFindText)
.Text = vFindText(i)
.Replacement.Text = sReplText
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
Set rngFind = ActiveDocument.Range
Next i
End With

Options.DefaultHighlightColorIndex = sHighlight

rngStart.Select
 
S

StevenM

To: Raj,

I don't know why, and perhaps there is yet a better solution, but you might
try:

Sub HighlightWords()
Dim vFindText As Variant
Dim sReplText As String
Dim sHighlight As String
Dim i As Long
sHighlight = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdBrightGreen
vFindText = Array("big", "small", "start", "finish")
sReplText = ""
For i = LBound(vFindText) To UBound(vFindText)
With Selection.Find
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Format = True
.MatchCase = False
.Text = vFindText(i)
.Replacement.Text = sReplText
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
End With
Next i
Options.DefaultHighlightColorIndex = sHighlight
End Sub

Steven Craig Miller
 
R

Raj

To: Raj,

I don't know why, and perhaps there is yet a better solution, but you might
try:

Sub HighlightWords()
    Dim vFindText As Variant
    Dim sReplText As String
    Dim sHighlight As String
    Dim i As Long
    sHighlight = Options.DefaultHighlightColorIndex
    Options.DefaultHighlightColorIndex = wdBrightGreen
    vFindText = Array("big", "small", "start", "finish")
    sReplText = ""
    For i = LBound(vFindText) To UBound(vFindText)
        With Selection.Find
            .Forward = True
            .Wrap = wdFindContinue
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Format = True
            .MatchCase = False
            .Text = vFindText(i)
            .Replacement.Text = sReplText
            .Replacement.Highlight = True
            .Execute Replace:=wdReplaceAll
        End With
    Next i
    Options.DefaultHighlightColorIndex = sHighlight
End Sub

Steven Craig Miller












- Show quoted text -

Hi Jean-Guy and Steven,

Thanks a lot. Both solutions worked.

Regards,
Raj
 
S

StevenM

To: Raj,

While it doesn't seem to make a whole lot of difference, you might consider
changing: Dim sHighlight As String
To: dim nHighlight as Long

And then change the 's' to 'n' for the Highlight variable.

If you do a search in help for DefaultHighlightColorIndex, you'll see that
it returns a long variable. But it seems to work either way.

Steven Craig Miller
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top