Word Macro problem : character substitution from one font to another

S

Stephen Miller

I wish to create a simple macro (don't we all?) that will
replace all the digits in Adobe Garamond with the digits in
Adobe Garamond Expert (basically, I want old style figures
in my documents).

So I *recorded* a macro which turns out so:

Sub Macro1()
'
' Macro1 Macro
' Macro recorded 20/1/04 by Stephen Miller
'
Selection.Find.ClearFormatting
With Selection.Find.Font
..Size = 10
..Bold = False
..Italic = False
End With
With Selection.Find
..Text = "1"
..Replacement.Text = ""
..Forward = True
..Wrap = wdFindContinue
..Format = True
..MatchCase = False
..MatchWholeWord = False
..MatchWildcards = False
..MatchSoundsLike = False
..MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Find.ClearFormatting
With Selection.Find.Font
..Size = 10
..Bold = False
..Italic = False
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
..Size = 10
..Bold = False
..Italic = False
End With
With Selection.Find
..Text = "1"
..Replacement.Text = "1"
..Forward = True
..Wrap = wdFindContinue
..Format = True
..MatchCase = False
..MatchWholeWord = False
..MatchWildcards = False
..MatchSoundsLike = False
..MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

I am no rocket scientist but even I know that this will not
work as the macro has recorded *nothing* about the fonts so
when it is run it simply replaces "1" in the current font
with, er, "1" in the current font!

Can anyone help with the magic needed to invoke the
particular or for that matter any font I choose?

Thanks
Stephen Miller
 
J

Jay Freedman

Hi, Stephen,

The macro recorder isn't exactly the brightest bulb on the Christmas tree...

Using the .Font.Name and .Replacement.Font.Name parameters gets you the
change you want (it doesn't care what the font size, bold, or italic
settings are). Using "^#" in the .Text parameter limits the changes to
digits, and using "^&" in the .Replacement.Text parameters means to replace
with the same character that was found. Finally, using Replace:=wdReplaceAll
does all the replacements in one step.

Sub SwitchFont()
Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = True
.Font.Name = "Monotype Corsiva"
.Replacement.Font.Name = "Arial Black"
.Text = "^#"
.Replacement.Text = "^&"
.Forward = True
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
End Sub
 
S

Stephen Miller

-----Original Message-----
Hi, Stephen,

The macro recorder isn't exactly the brightest bulb on the Christmas tree...

Using the .Font.Name and .Replacement.Font.Name parameters gets you the
change you want (it doesn't care what the font size, bold, or italic
settings are). Using "^#" in the .Text parameter limits the changes to
digits, and using "^&" in the .Replacement.Text parameters means to replace
with the same character that was found. Finally, using Replace:=wdReplaceAll
does all the replacements in one step.

[snip]

Fantastic, and it works as well! But only on the body of
the text and not in footnotes nor endnotes...

Stephen Miller
 
J

Jay Freedman

Stephen said:
-----Original Message-----
Hi, Stephen,

The macro recorder isn't exactly the brightest bulb on the Christmas
tree...

Using the .Font.Name and .Replacement.Font.Name parameters gets you
the change you want (it doesn't care what the font size, bold, or
italic settings are). Using "^#" in the .Text parameter limits the
changes to digits, and using "^&" in the .Replacement.Text
parameters means to replace with the same character that was found.
Finally, using Replace:=wdReplaceAll does all the replacements in
one step.

[snip]

Fantastic, and it works as well! But only on the body of
the text and not in footnotes nor endnotes...

Stephen Miller

For the extra code you need to search through all the other parts of the
document, see http://word.mvps.org/FAQs/MacrosVBA/FindReplaceAllWithVBA.htm.
 

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