Find and replace font specs with macro

N

Nexan

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!
 
G

Greg

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.
 
N

Nexan

Greg,

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

Thanks!
 
G

Graham Mayor

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

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
G

Greg

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
 
N

Nexan

That worked perfectly. Thanks!

Graham Mayor said:
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

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - 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