Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2005

Tip: Looking for answers? Try searching our database.

Find and replace font specs with macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Nexan - 06 Apr 2005 15:25 GMT
First, my apologies on two counts: one, that I'm sure this is really basic,
and two, that I think I may have asked this before but can't find and/or
recall the answer.

All I'm trying to do is to create a macro that will find a given string of
text and change it to bold and underline (single).

Thanks in advance!
Greg - 06 Apr 2005 15:34 GMT
Nexan,

First,  you can do this without a macro with Word's Find and Replace
dialog.  Edit>Repalce opens the dialog.  Type the string in the find
what field.  Type ^& in the replace with field.  Click More>Format>Font
apply bold and single underline

Click replace all.

If you still want a macro, post back.
Nexan - 06 Apr 2005 15:53 GMT
Greg,

No, it definitely needs to be in macro format, since the action is going to
be part of a larger macro.

Thanks!

> Nexan,
>
[quoted text clipped - 6 lines]
>
> If you still want a macro, post back.
Graham Mayor - 06 Apr 2005 16:19 GMT
In that case - as Greg has just told me he is going to a meeting -

 Selection.HomeKey Unit:=wdStory
   Selection.Find.ClearFormatting
   Selection.Find.Replacement.ClearFormatting
       With Selection.Find
'**********************
       .Text = "text" 'put your searched text here
       .Replacement.Text = "^&"
       .Replacement.Font.Underline = True
       .Replacement.Font.Bold = True
'**********************
       .Forward = True
       .Wrap = wdFindContinue
       .Format = True
       .MatchCase = False
       .MatchWholeWord = False
       .MatchAllWordForms = False
       .MatchSoundsLike = False
       .MatchWildcards = False
   End With
   Selection.Find.Execute replace:=wdReplaceAll

Signature

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web site www.gmayor.com
Word MVP web site http://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

> Greg,
>
[quoted text clipped - 13 lines]
>>
>> If you still want a macro, post back.
Greg - 06 Apr 2005 16:36 GMT
It was a short meeting.  A rare thing around here :-)
Nexan - 06 Apr 2005 17:01 GMT
That worked perfectly. Thanks!

> In that case - as Greg has just told me he is going to a meeting -
>
[quoted text clipped - 36 lines]
> >>
> >> If you still want a macro, post back.
Greg - 06 Apr 2005 16:27 GMT
Nexan,

Something like:

Public Sub BasicFindReplaceWithVBA()

Dim rngstory As Word.Range
Dim findText As String
Dim replacementText As String
Dim Response As VbMsgBoxResult

findText = "Your String"
replacementText = "^&"

' Fix the skipped blank Header/Footer problem
MakeHFValid
' Iterate through all story types in the current document
For Each rngstory In ActiveDocument.StoryRanges
' Iterate through all linked stories
 Do
   SearchAndAlterTextInStory rngstory, findText, replacementText
   ' Get next linked story (if any)
   Set rngstory = rngstory.NextStoryRange
 Loop Until rngstory Is Nothing
Next
End Sub
Public Sub SearchAndAlterTextInStory(ByVal rngstory As Word.Range, _
ByVal strSearch As String, ByVal strReplace As String)

ResetFRParameters
'This routine supplied by Peter Hewett
Do Until (rngstory Is Nothing)
 With rngstory.Find
   .ClearFormatting
   .Replacement.ClearFormatting
   .Text = strSearch
   With .Replacement
     .Text = strReplace
     .Font.Bold = True
     .Font.Underline = wdUnderlineSingle
   End With
   .Execute Replace:=wdReplaceAll
 End With
 Set rngstory = rngstory.NextStoryRange
Loop
End Sub
Public Sub MakeHFValid()

Dim lngJunk As Long
' It does not matter whether we access the Headers or Footers property.
' The critical part is accessing the stories range object
lngJunk = ActiveDocument.Sections(1).Headers(1).Range.StoryType
End Sub
Sub ResetFRParameters()

With Selection.Find
 .ClearFormatting
 .Replacement.ClearFormatting
 .Text = ""
 .Replacement.Text = ""
 .Forward = True
 .Wrap = wdFindContinue
 .Format = False
 .MatchCase = False
 .MatchWholeWord = True
 .MatchWildcards = False
 .MatchSoundsLike = False
 .MatchAllWordForms = False
End With

End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.