Macro for search and replace not working

B

Brad in Tampa

I have MS Word 2002. I am trying to create a macro with a search and replace
for Times New Roman size 12 to replace it with Courier New size 10. It works
when creating the macro, but not when I run the macro. And there is no
mention of either font in the VBA (edit). Any ideas?

Thanks,
brad
 
B

Brad in Tampa

Ok, I found a solution. When changing font and size of font in a macro,
don't use search and replace. Instead highlight area and use "Format" and
then "Font" from the toolbar.
 
G

Graham Mayor

The macro recorder does not store the complete range of functions and often
produces rather bulky code. To add font information to a search, you will
have to add it manually eg

Sub ReplaceExample()
With Selection
.HomeKey Unit:=wdStory
With .Find
.ClearFormatting
.Replacement.ClearFormatting
'**********************
.Text = ""
.Font.Name = "Times New Roman"
.Font.Size = "12"
.Replacement.Text = ""
.Replacement.Font.Name = "Courier New"
.Replacement.Font.Size = "10"
'**********************
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With
End With
End Sub

http://www.gmayor.com/installing_macro.htm
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
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