How highlighting all found items with a macro?

A

avkokin

Hello. The Dialog Box of "Find and Replace" (Tab "Find") including the
field with the text "Highlight all items found in...". I need to
select all found words with help macro but I not know how I do it. I
wrote code of searching and highlighting next item but not all items:

With Selection.Find
.ClearFormatting
.Text = Selection.Text
.Replacement.ClearFormatting
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.Execute
End With

How highlighting all found items? Thank you very much.
 
H

Helmut Weber

Hi Avkokin,

like that:

Sub Test6()
Dim rTmp As Range
Set rTmp = ActiveDocument.Range
Options.DefaultHighlightColorIndex = wdRed
With rTmp.Find
.Text = "lazy"
.Replacement.Text = "lazy"
.Replacement.Highlight = True
.Execute Replace:=wdReplaceAll
End With
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
G

Graham Mayor

Do you want to *highlight* ie with a colour, all items found? In which case

Sub ReplaceExample()
Dim sFindText As String
sFindText = InputBox("Find what?")
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
'**********************
.Text = sFindText
.Replacement.Text = "^&"
.Replacement.Highlight = True
'**********************
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.Execute replace:=wdReplaceAll
End With
End With
End Sub

or

Do you want to *select* all items found? The Find dialog does this, so why
do you need to re-invent the wheel? If you want to process the found items
then a macro would do so individually.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

avkokin

Do you want to *highlight* ie with a colour, all items found? In which case

Sub ReplaceExample()
Dim sFindText As String
sFindText = InputBox("Find what?")
    With Selection
        .HomeKey Unit:=wdStory
        With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
'**********************
            .Text = sFindText
            .Replacement.Text = "^&"
            .Replacement.Highlight = True
'**********************
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchAllWordForms = False
            .MatchSoundsLike = False
            .MatchWildcards = False
            .Execute replace:=wdReplaceAll
        End With
    End With
End Sub

or

Do you want to *select* all items found? The Find dialog does this, so why
do you need to re-invent the wheel? If you want to process the found items
then a macro would do so individually.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>






- Show quoted text -

Yes, I just want to *select* all items found but not color. Thank you
for your help.
 
A

avkokin

Yes, I just want to *select* all items found but not color. Thank you
for your help.- Hide quoted text -

- Show quoted text -

And I want to do it with my macro but not use all times dialog box.
 
H

Helmut Weber

Hi Anton,

I can't find a way to set the option
"highlight all ..." in a macro.

It isn't of much use anyway,
as this so called discontiguous selection
can not, in my humble opinion, be accessed programmatically.

Try
msgbox selection.words.count

Whatever number of words is selected,
the msgbox always returns "1",
of course, if at least one string was found.

Therefore, what Graham and I suggested,
might be preferable.
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

Jay Freedman

Hi Anton,

I can't find a way to set the option
"highlight all ..." in a macro.

It isn't of much use anyway,
as this so called discontiguous selection
can not, in my humble opinion, be accessed programmatically.

Try
msgbox selection.words.count

Whatever number of words is selected,
the msgbox always returns "1",
of course, if at least one string was found.

Therefore, what Graham and I suggested,
might be preferable.

You're exactly correct, Helmut. VBA has very little understanding of
discontiguous selections (http://support.microsoft.com/?kbid=288424) and it
can't create them at all.
 

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