How to: Programmatically Format Text in Documents

 

You can use the Range object to format text in a Microsoft Office Word document.

Applies to: The information in this topic applies to document-level projects and VSTO add-in projects for Word. For more information, see Features Available by Office Application and Project Type.

The following example selects the first paragraph in the document and changes the font size, the font name, and the alignment. It then selects the range and displays a message box to pause before executing the next section of code. The next section calls the Undo method of the Microsoft.Office.Tools.Word.Document host item (for a document-level customization) or the Microsoft.Office.Interop.Word.Document class (for a VSTO Add-in) three times. It applies the Normal Indent style and displays a message box to pause the code. Then the code calls the Undo method once, and displays a message box.

To format text using a document-level customization

  1. The following example can be used in a document-level customization. To use this code, run it from the ThisDocument class in your project.

    No code example is currently available or this language may not be supported.

To format text using a VSTO Add-in

  1. The following example can be used in a VSTO Add-in. This example uses the active document. To use this code, run it from the ThisAddIn class in your project.

    No code example is currently available or this language may not be supported.

How to: Programmatically Define and Select Ranges in Documents
How to: Programmatically Insert Text into Word Documents
How to: Programmatically Search for and Replace Text in Documents

Show: