Programmatically add text and formatting to cells in Word tables
Applies to: Visual Studio Visual Studio for Mac Visual Studio Code
Each table consists of a collection of cells. Each individual Cell object represents one cell in the table. You refer to each cell by its location in the table. This example refers to the cell located in the first row and the first column of the table; adds text to the cell; and applies formatting.
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.
To add text and formatting to cells
Refer to the cell by its location in the table, add text to the cell, and apply the formatting.
The following code example can be used in a document-level customization. To use this example, run it from the
ThisDocumentclass in your project.
Word.Cell cell = this.Tables.Cell(1, 1); cell.Range.Text = "Name"; cell.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
The following code example can be used in a VSTO Add-in. This example uses the active document. To use the example, run it from the
ThisAddInclass in your project.