Highlighting a word in the entire document

M

M.L.Srinivas

Hi,

I want to find a word and highlight the word (using
fillcolor) in the document.
I used:
With Selection.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Text = strToFind
.Execute
End With
but this is finding only the first instance of the given
word, I want all the instances of the word to highlighted.

And also how to highlight the found word using fillcolor.

Please help.

Any sort of help is appreciated.

Thanks

Srinivas
 
D

Doug Robbins

Use the following:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="fox", MatchWildcards:=False,
Wrap:=wdFindStop, Forward:=True) = True
Selection.Range.HighlightColorIndex = wdYellow
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
N

Nishanth

Hi,
Maybe the following code will help

With Selection.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Text = strToFind
Do While .Execute
activedocument.selection.range.HighlightColorIndex = wdRed
activedocument.Selection.EndKey Unit:=wdLine
activedocument.Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Loop
End With
 
M

M.L.Srinivas

Hi Nishant,

Thanks for your time.

Your code helped me alot.

Thanks
Srinivas
-----Original Message-----
Hi,
Maybe the following code will help

With Selection.Find
.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Text = strToFind
Do While .Execute
activedocument.selection.range.HighlightColorIndex = wdRed
activedocument.Selection.EndKey Unit:=wdLine
activedocument.Selection.EndKey
Unit:=wdStory, Extend:=wdExtend
 
M

M.L.Srinivas

Hi,

This code is going to a never ending loop. In another Mr.
Nishant suggested another code, with couple of
modifications. It worked fine.

Thanks for time and suggestion.

Srinivas
 
D

Doug Robbins

Not for me it didn't.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

Jean-Guy Marcil

M.L.Srinivas was telling us:
M.L.Srinivas nous racontait que :
Hi Nishant,

Thanks for your time.

Your code helped me alot.

Careful, there is a slight error in the code.
As is, if you have the word you are looking for more than one on any given
line, only the first one will be highlighted. Also, the code will not work
because Selection is not a property of the document object (i.e.
ActiveDocument.Selection will not compile).

So, use this version inead:

'_______________________________________
With Selection
With .Find
.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Text = "fox"
End With
Do While .Find.Execute
.Range.HighlightColorIndex = wdRed
.Collapse wdCollapseEnd
.EndKey Unit:=wdStory, Extend:=wdExtend
Loop
.Collapse wdCollapseEnd
End With
'_______________________________________

But, Doug's code is by far more efficient, and if used as is does not create
an infinite loop. If it did it means that you modified it or you have
surrounding code that modifies the way the code Doug posted behaves:

'_______________________________________
With Selection
.HomeKey wdStory
.Find.ClearFormatting
With .Find
Do While .Execute(FindText:="fox", MatchWildcards:=False, _
Wrap:=wdFindStop, Forward:=True) = True
Selection.Range.HighlightColorIndex = wdYellow
Loop
End With
End With
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

Dian D. Chapman, MVP

H

Helmut Weber

Hi everybody,

if efficiency is concerned,
how about this one:

Sub test6903()
Dim oRng As Range
ResetSearch
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "und"
.MatchWholeWord = True
While .Execute
oRng.HighlightColorIndex = wdYellow
Wend
End With
ResetSearch
End Sub

Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
' plus some more if needed
.Execute
End With
End Sub

Have a nice day.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
M

MLSrinivas

Hi Jean-Guy,

I observed the code flaws in Nishant code. I made the necessary changes
before I used it in my application. But Nishant's code had given the
lead to proceed further.

I don't know why Doug's code didn't work for me. I did not make any
changes in his code.

Anyway thanks for u all for your valuable time.

I need some more help.

How to find out the position of a word in a document. My task is to
locate owrds, 'He' or 'She' and color in yellow and red. The first
occuring word should be yellow and the next to be red. So, how to find
out the which is occurring first in the document.

Please help.


Srinivas
 
H

Helmut Weber

Hi,

Like this for alternating colors:

Sub Makro1()
Dim oRng As Range
ResetSearch
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "er"
.MatchWholeWord = True
While .Execute
oRng.HighlightColorIndex = wdYellow
.Execute
oRng.HighlightColorIndex = wdRed
Wend
End With
ResetSearch
End Sub

See my former posting for "Resetseach"

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
G

Greg

I am not JGM, but might be able to help just the same.

This code may be a little crude as I put it together without a great
deal of testing. You can use Range.Start to determine if he or she is
the first occurrence.

Something like:

Sub ScratchMacro()
Dim i As Integer
Dim j As Integer
Dim oRng As Word.Range

Set oRng = ActiveDocument.Range
With oRng.Find
.MatchWholeWord = True
.Text = "he"
.Execute
i = oRng.Start
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.MatchWholeWord = True
.Text = "she"
.Execute
j = oRng.Start
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.MatchWholeWord = True
If i < j Then
.Text = "he"
Else
.Text = "she"
End If
While .Execute
oRng.Font.Color = wdColorYellow
Wend
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.MatchWholeWord = True
If i > j Then
.Text = "he"
Else
.Text = "she"
End If
While .Execute
oRng.Font.Color = wdColorRed
Wend
End With
End Sub
 
H

Helmut Weber

Hi,

corrected version:

Sub Makro1()
Dim oRng As Range
ResetSearch
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "er"
.MatchWholeWord = True
While .Execute
oRng.HighlightColorIndex = wdYellow
If .Execute Then ' !!!
oRng.HighlightColorIndex = wdRed
End If
Wend
End With
ResetSearch
End Sub

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 

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