A Microsoft Office (Excel, Word) forum. OfficeFrustration

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.

Go Back   Home » OfficeFrustration forum » Microsoft Word » Formatting Long Documents
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

[vba] search and format finds



 
 
Thread Tools Display Modes
  #1  
Old June 24th, 2005, 07:28 PM
leaftye leaftye is offline
Member
 
First recorded activity by OfficeFrustration: Jun 2005
Location: San Diego
Posts: 11
Send a message via AIM to leaftye Send a message via Yahoo to leaftye
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
  #2  
Old June 24th, 2005, 10:40 PM
leaftye leaftye is offline
Member
 
First recorded activity by OfficeFrustration: Jun 2005
Location: San Diego
Posts: 11
Send a message via AIM to leaftye Send a message via Yahoo to leaftye
Default

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  
Old June 25th, 2005, 06:52 AM
Doug Robbins
external usenet poster
 
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



 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

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


All times are GMT +1. The time now is 05:52 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.