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!

Re: Find and replace font specs with macro by Greg

Greg
Wed Apr 06 09:34:44 CDT 2005

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.


Re: Find and replace font specs with macro by Nexan

Nexan
Wed Apr 06 09:53:09 CDT 2005

Greg,

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

Thanks!

"Greg" wrote:

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

Re: Find and replace font specs with macro by Graham

Graham
Wed Apr 06 10:19:33 CDT 2005

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

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



Nexan wrote:
> Greg,
>
> No, it definitely needs to be in macro format, since the action is
> going to be part of a larger macro.
>
> Thanks!
>
> "Greg" wrote:
>
>> Nexan,
>>
>> First, you can do this without a macro with Word's Find and Replace
>> dialog. Edit>Replace 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.



Re: Find and replace font specs with macro by Greg

Greg
Wed Apr 06 10:27:46 CDT 2005

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


Re: Find and replace font specs with macro by Greg

Greg
Wed Apr 06 10:36:24 CDT 2005

It was a short meeting. A rare thing around here :-)


Re: Find and replace font specs with macro by Nexan

Nexan
Wed Apr 06 11:01:07 CDT 2005

That worked perfectly. Thanks!

"Graham Mayor" wrote:

> 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
>
> My web site www.gmayor.com
> Word MVP web site http://word.mvps.org
> <>>< ><<> ><<> <>>< ><<> <>>< <>><<>
>
>
>
> Nexan wrote:
> > Greg,
> >
> > No, it definitely needs to be in macro format, since the action is
> > going to be part of a larger macro.
> >
> > Thanks!
> >
> > "Greg" wrote:
> >
> >> Nexan,
> >>
> >> First, you can do this without a macro with Word's Find and Replace
> >> dialog. Edit>Replace 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.
>
>
>