How to: Add Text and Formatting to Cells in Word Tables

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

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.

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 ThisDocument class in your project.

    With Me.Tables.Item(1).Cell(1, 1).Range
        .Text = "Name"
        .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    End With
    
    Word.Cell cell = this.Tables[1].Cell(1, 1);
    
    cell.Range.Text = "Name"; 
    cell.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
    

    The following code example can be used in an application-level add-in. This example uses the active document. To use the example, run it from the ThisAddIn class in your project.

    With Me.Application.ActiveDocument.Tables.Item(1).Cell(1, 1).Range
        .Text = "Name"
        .ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    End With
    
    Word.Cell cell = this.Application.ActiveDocument.Tables[1].Cell(1, 1);
    
    cell.Range.Text = "Name";
    cell.Range.ParagraphFormat.Alignment = 
        Word.WdParagraphAlignment.wdAlignParagraphRight;
    

See Also

Tasks

How to: Create Word Tables

How to: Add Rows and Columns to Word Tables

How to: Populate Word Tables with Document Properties