Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ms-word

vba

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?

like image 628
CrimsonX Avatar asked Jan 24 '26 04:01

CrimsonX


1 Answers

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
like image 139
Todd Main Avatar answered Jan 25 '26 23:01

Todd Main



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!