vba code to select all occurrences of some text

R

robot

Hello,

I am using Word XP, which allows users the option to "highlight all items
found" in the Find dialog box.

I record a macro for such an action, but the macro would only highlight the
first found item.

l look through the Help file, but could not find any properties/methods of
the Find object that would highlight all found items.

Suggestions are most welcome.
 
H

Helmut Weber

Hi Robot,

You may utilize highlightcolorindex,
which is a bit more complicated for a beginner,
but in the end, I think, you can use it like
a discontiguous selection.

Sub MyHighlight()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Text = "brown"
While .Execute
rDcm.HighlightColorIndex = wdYellow
Wend
End With
End Sub

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
K

Klaus Linke

robot said:
Hello,

I am using Word XP, which allows users the option to "highlight all items
found" in the Find dialog box.

I record a macro for such an action, but the macro would only highlight
the
first found item.

l look through the Help file, but could not find any properties/methods of
the Find object that would highlight all found items.

Suggestions are most welcome.

The only way to do it would be using SendKeys (which is pretty kludgy and
error-prone):

SendKeys "{TAB}+{TAB}{TAB}{TAB}{ENTER}"
With Dialogs(wdDialogEditFind)
.Find = "e"
.Display 1
End With

Klaus
 
K

Klaus Linke

SendKeys "{TAB}+{TAB}{TAB}{TAB}{ENTER}"
With Dialogs(wdDialogEditFind)
.Find = "e"
.Display 1
End With

The "+" should have been in braces {+}, but SendKeys still lives up to its
reputation and doesn't work.

SendKeys "%t{+}{TAB}{TAB}{TAB}{ENTER}"
should work (more or less reliably) in the English version.

Klaus
 

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