If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
[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 |
#2
|
|||
|
|||
I figured it out. Here's what my final code looks like:
Code:
Sub UnderlineWords() ' Underline all the words in this array Dim WordsToUnderline(10) As String WordsToUnderline(0) = "access" WordsToUnderline(1) = "assignment" For i = 0 To UBound(WordsToUnderline) With Selection.Find .ClearFormatting With .Replacement .ClearFormatting .Font.Underline = wdUnderlineSingle .Text = WordsToUnderline(i) End With .Text = WordsToUnderline(i) .Forward = True .Wrap = wdFindContinue .Format = True .Execute Replace:=wdReplaceAll End With Next End Sub |
#3
|
|||
|
|||
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 |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Search a cell based on format | Al | General Discussion | 4 | May 19th, 2005 07:02 PM |
Search and Replace not working if I select a Format for Heading St | Sherrie Deen | General Discussion | 0 | April 27th, 2005 08:53 PM |
DCount and Date format Question Against IBM DB2 Date | RNUSZ@OKDPS | Running & Setting Up Queries | 2 | March 7th, 2005 09:19 PM |
data access pages: how to create search engine & guestbook ??? (access 2000) | Jamie Carpenter via AccessMonster.com | Using Forms | 0 | March 1st, 2005 09:02 PM |
Omnipage Pro 14 - How do I save OCR data in TIFF so I can search document with Home XP Documents search? | CSM1 | General Discussions | 0 | November 5th, 2004 12:34 AM |