Working with Character and Paragraph Styles in Word

Office 2010

Office Quick Note banner

Working with Styles in Microsoft Word: Learn how to programmatically change the character and paragraph styles in a Microsoft Word 2010 document.

Last modified: May 12, 2011

Applies to: Office 2010 | VBA | Word 2010

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

Published:   May 2011

Provided by:    Frank Rice, Microsoft Corporation

Styles are one of the most important and valuable tools in Microsoft Word. A style is a named collection of formatting commands; each style enables you to easily access and apply a given set of commands as a group. When you use styles to format a document, it is easy to change the formatting in a long document by changing it only once in the style definition. After you change the formatting in the style definition, Word automatically applies those changes to all of the text in the document that uses that style. In this topic, you programmatically change character and paragraph styles in a document. To complete this task, you must do the following:

In this task, you add programming code that changes the styles of text in a Word document and then writes the style names to the Immediate window.

To add code to the Visual Basic Editor

  1. Start Word 2010.

  2. On the Developer tab, click Visual Basic to open the Visual Basic Editor.

    Note Note

    If you do not see the Developer tab in Word 2010, click the File tab, and then click Options. In the categories pane, click Custom Ribbon, select Developer, and then click OK.

  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 RangeCharacterParagraphStyle()
        Dim rng As Range
        ' Select a character range that covers the first four words,
        ' using the sample random text:
        Set rng = ActiveDocument.Range
       
        rng.Words(3).Style = "Book Title"
        rng.Words(4).Style = "Emphasis"
        rng.Paragraphs(1).Style = "Heading 3"
       
        Dim charStyle As Style
       
        ' Note that attempting to retrieve the CharacterStyle
        ' property of a range that includes multiple character
        ' styles returns Nothing:
        Set charStyle = rng.CharacterStyle
        ' This should display True in the Immediate window:
        Debug.Print charStyle Is Nothing
       
        ' This should display "Book Title" in the Immediate window:
        Set charStyle = rng.Words(3).CharacterStyle
        Debug.Print charStyle.NameLocal
       
        ' This should display "Emphasis" in the Immediate window:
        Set charStyle = rng.Words(4).CharacterStyle
        Debug.Print charStyle.NameLocal
       
        Dim paraStyle As Style
       
        ' Note that attempting to retrieve the ParagraphStyle
        ' property of a range that includes multiple paragraph
        ' styles returns Nothing:
        Set paraStyle = rng.ParagraphStyle
       
        ' This should display True in the Immediate window:
        Debug.Print paraStyle Is Nothing
    
        ' This should print Heading 3 in the Immediate window.    
        Set paraStyle = rng.Paragraphs(1).Range.ParagraphStyle
        Debug.Print paraStyle.NameLocal
    End Sub
    
    

In this task, you add random text to the document and then run the VBA code. The code changes the character and paragraph styles and then writes to the Immediate window.

To run the code

  1. In the document, type the following command (without the quotation marks). This adds one paragraph of five sentences to the document.

    "=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 your monitor and adjust the windows until you can see them both.

  4. Press F8 to step through the code line-by-line and watch the code change the styles in the document.

Show:
© 2014 Microsoft