Reply
 
Thread Tools Display Modes
  #1   Report Post  
leaftye
 
Posts: n/a
Default [vba] search and format finds


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

--------------------


--
leaftye
  #2   Report Post  
Doug Robbins
 
Posts: n/a
Default

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
"leaftye" wrote in message
...

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

--------------------


--
leaftye



Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
different "number format" for the same "level heading" Waleed Microsoft Word Help 3 June 26th 05 05:15 AM
Search and Replace not working if I select a Format for Heading St Sherrie Deen Microsoft Word Help 0 April 27th 05 08:53 PM


All times are GMT +1. The time now is 04:31 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"