[vba] search and format finds

L

leaftye

I'm trying to create a procedure that searches for certain words and
then formats those words. Two problems:
1. If I have a word(s) highlighted when I start the macro, it formats
the selection...even though it hasn't found anything yet.
2. It finds the first word, but not the rest.

Please help me figure out what I'm doing wrong. Here's my code:


Code:
--------------------

With Selection
With .Find
.ClearFormatting
.Text = "access"
.MatchCase = False
.MatchWholeWord = True
.Execute
.ClearFormatting
End With
.FormattedText.Case = wdTitleWord
.Font.Underline = wdUnderlineSingle
End With

With Selection
With .Find
.ClearFormatting
.Text = "assignment"
.MatchCase = False
.MatchWholeWord = True
.Execute
.ClearFormatting
End With
.FormattedText.Case = wdTitleWord
.Font.Underline = wdUnderlineSingle
End With
 
D

Doug Robbins

Here's how to do it:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="access", MatchWildcards:=False,
MatchCase:=False, _
MatchWholeWord:=True, Wrap:=wdFindStop, Forward:=True) = True
Selection.Range.Case = wdTitleWord
Loop
End With
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="assignment", MatchWildcards:=False,
MatchCase:=False, _
MatchWholeWord:=True, Wrap:=wdFindStop, Forward:=True) = True
Selection.Range.Case = wdTitleWord
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
 

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