CrimsonX
CrimsonX

Reputation: 9238

Word 2003 - How to use a Macro to Change styles?

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?

Upvotes: 3

Views: 9029

Answers (2)

Dirk Vollmar
Dirk Vollmar

Reputation: 176159

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)

Upvotes: 2

Todd Main
Todd Main

Reputation: 29153

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

Upvotes: 4

Related Questions