Working with Text Formatting and Styles in Word 2010
Working with Styles in Microsoft Word: Learn how to work with text formatting and styles in Microsoft Word 2010.
Last modified: May 20, 2011
Applies to: Office 2010 | VBA | Word 2010
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:
In this task, you add programming code that manipulates character and paragraph formatting.
To add code to the Visual Basic Editor
Press Alt+F11 to open the Visual Basic Editor.
In the Projects pane, click ThisDocument.
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
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
In the document, type the following command (without the quotes) to add one paragraph of five sentences.
Drag the Visual Basic Editor window to the right side of your monitor.
Drag the Word window to the left side of the monitor and adjust the windows until you can see them both.
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.