views:

270

answers:

2

Is it possible to create a Word 2003 Macro to change the font style of certain segments of a document?

For example, say I have a document that has a large portion of text as bold italic and 12 point font. I'd like to replace all text with these characteristics with underlined 14 point font.

I've already done some searches on Google, StackOverflow and Microsoft's website but I haven't been able to find anything that discusses if this is even possible.

Any help?

+1  A: 

As always, Word's macro recorder can be of great help:

Selection.Find.ClearFormatting
With Selection.Find.Font
    .Size = 12
    .Bold = True
    .Italic = True
End With
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
    .Size = 14
    .Bold = False
    .Italic = False
    .Underline = wdUnderlineSingle
End With
With Selection.Find
    .Text = ""
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

(To generate the macro start the macro recorder, press Ctrl+H to open Find & Replace and then specify the appropriate find format and replacment format)

0xA3
I'm glad macros have programmatic access to the replacement.text command, thanks for the help!
CrimsonX
+2  A: 

Yeah, you'll want to use the .Find object and it's child .Replacement content. You can do this on a Selection (limited run), a Range (paragraphs, stories, etc.) or the whole document. The sample below is for the whole document (ActiveDocument.Content).

Sub FindReplaceStyle()
    With ActiveDocument.Content.Find
        .ClearFormatting

        With .Font
            .Bold = True
            .Size = 14
            .Italic = True
        End With

        .Format = True

        With .Replacement
            .ClearFormatting
            With .Font
                .Bold = False
                .Italic = False
                .Underline = wdUnderlineSingle
                .Size = 12
            End With
        End With

        .Execute Forward:=True, Replace:=wdReplaceAll, _
            FindText:="", ReplaceWith:=""
    End With
End Sub
Otaku
Very Nice, thanks! I will try this out later today (by Monday at the latest). I appreciate the help!
CrimsonX
@CrimsonX: Just wanted to check in with you to see if you've had a chance to test it out.
Otaku
@Otaku - its on my radar, but I expect I'll use the macro early next week. Possibly later than Monday, but I expect by Wednesday. Thanks for the help, sorry for the delay.
CrimsonX
no problem, here to help!
Otaku