Request for macro to highlight selected text

S

sandeep6699

Greetings,

I am requesting help with a simple VBA macro that prompts the user for some text and then highlights all occurrences of that text in the document. It would be nice if the pattern accepts wildcards as well.

Thanks in advance.

Regards,
Sandeep
 
A

Auric__

sandeep6699 said:
I am requesting help with a simple VBA macro that prompts the user for
some text and then highlights all occurrences of that text in the
document. It would be nice if the pattern accepts wildcards as well.

I'm assuming you want this done in Word.

Sub FingAndHilite()
usertext = InputBox("Enter text to find")
If Len(usertext) < 1 Then Exit Sub
With Selection.Find
.ClearFormatting
.Text = usertext
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Do
With Selection.Find
.Execute
If Not .Found Then Exit Do
End With
Selection.Range.HighlightColorIndex = wdYellow
Loop
End Sub

Note that this is not a highlight like selecting text, but is a change made
to the document itself. This might annoy some users.
 
A

Auric__

I said:
I'm assuming you want this done in Word.

Sub FingAndHilite()
usertext = InputBox("Enter text to find")
If Len(usertext) < 1 Then Exit Sub

Forgot to mention, this starts from the cursor's current position. To start
from the beginning of the document, add this after the above lines:

Selection.HomeKey Unit:=wdStory
 
S

sandeep6699

Forgot to mention, this starts from the cursor's current position. To start

from the beginning of the document, add this after the above lines:



Selection.HomeKey Unit:=wdStory

Excellent stuff ---- works like a charm. God Bless!
 

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