Share via


Working with Text Formatting and Styles in Word 2010

Office Quick Note banner

Working with Styles in Microsoft Word: Learn how to work with text formatting and styles in Microsoft Word 2010.

Applies to: Office 2010 | VBA | Word 2010

In this article
Add the Code to the Visual Basic Editor
Test the Solution
Next Steps

Published:   June 2011

Provided by:    Frank Rice, Microsoft Corporation

Text formatting consists of changing individual characters, words, single paragraphs, or pages. This enables you to effectively change the look of your text by applying different fonts, sizes, and styles such as making the text appear bold or italicized. In this topic, you programmatically use the Selection.ClearCharacter and Selection.ClearParagraph methods to change the formatting and style of a text string. To complete this task, you must do the following:

  • Add the Code to the Visual Basic Editor

  • Test the Solution

Add the Code to the Visual Basic Editor

In this task, you add programming code that manipulates character and paragraph formatting.

To add code to the Visual Basic Editor

  1. Start Word.

  2. Press Alt+F11 to open the Visual Basic Editor.

  3. In the Projects pane, click ThisDocument.

  4. Paste or type the following Microsoft Visual Basic for Applications (VBA) code into the module window.

    Sub SelectionClearFormattingDemo()
        ' Apply character and paragraph formats.
        ' Press F8 to step into this the first time.
        ' Press Shift+F8 to step over subsequent lines of code:
        ApplyFormattingAndSelect Sentences(3)
    
        ' Note that the sentence is now formatted.
        ' Remove the character direct formatting;
        Selection.ClearCharacterDirectFormatting
    
        ' Reapply the formatting:
        ApplyFormattingAndSelect Sentences(3)
    
        ' Remove the character style formatting:
        Selection.ClearCharacterStyle
    
        ' Reapply the formatting:
        ApplyFormattingAndSelect Sentences(3)
    
        ' Remove all character formatting (leaving paragraph formatting):
        Selection.ClearCharacterAllFormatting
    
        ' Reapply the formatting:
        ApplyFormattingAndSelect Sentences(3)
    
        ' Remove the paragraph direct formatting:
        Selection.ClearParagraphDirectFormatting
    
        ' Reapply the formatting:
        ApplyFormattingAndSelect Sentences(3)
    
        ' Remove the paragraph style formatting:
        Selection.ClearParagraphStyle
    
        ' Reapply the formatting:
        ApplyFormattingAndSelect Sentences(3)
    
        ' Remove all paragraph formatting (leaving character formatting):
        Selection.ClearParagraphAllFormatting
    
    End Sub
    
    Sub ApplyFormattingAndSelect(rng As Range)
        With rng
            ' Apply a paragraph and character style:
            .Style = "Quote"
    
            ' Apply a character style:
            .Style = "Subtle Reference"
    
            ' Apply direct formatting:
            .Font.Bold = True
            .Font.ColorIndex = wdBrightGreen
            .ParagraphFormat.LineSpacing = 20
            .Select
        End With
    End Sub
    

Test the Solution

In this task, you add text to a document and then step through the VBA code to see the effects on text formatting and style.

To step through the code

  1. In the document, type the following command (without the quotes) to add one paragraph of five sentences.

    "=rand(1,5)"

  2. Drag the Visual Basic Editor window to the right side of your monitor.

  3. Drag the Word window to the left side of the monitor and adjust the windows until you can see them both.

  4. Now, place the cursor in SelectionClearFormattingDemo procedure, press F8 to get started debugging, and then press Shift+F8 to step through the code line-by-line (and bypass external procedures) and watch the code behavior.

Next Steps