Programmatically create Word tables

The Tables collection is a member of the Document, Document, Selection, and Range classes, which means that you can create a table in any of those contexts. You use the Add method of the Tables collection to add a table at the specified range.

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.

Create tables in document-level customizations

To add a table to a document

  • Use the Add method to add a table consisting of three rows and four columns at the beginning of the document.

    To use the following code example, run it from the ThisDocument class in your project.

    object start = 0;
    object end = 0;
    Word.Range tableLocation = this.Range(ref start, ref end);
    this.Tables.Add(tableLocation, 3, 4);
    

When you create a table, it is automatically added to the Tables collection of the Document host item. You can then refer to the table by its item number by using the Item[] property, as shown in the following code.

To refer to a table by item number

  1. Use the Item[] property and supply the item number of the table that you want to refer to.

    To use the following code example, run it from the ThisDocument class in your project.

    Word.Table newTable = this.Tables[1];
    

    Each Table object also has a Range property that enables you to set formatting attributes.

To apply a style to a table

  1. Use the Style property to apply one of the Word built-in styles to a table.

    To use the following code example, run it from the ThisDocument class in your project.

    this.Tables[1].Range.Font.Size = 8;
    this.Tables[1].set_Style("Table Grid 8");
    

Create tables in VSTO Add-ins

To add a table to a document

  • Use the Add method to add a table consisting of three rows and four columns at the beginning of the document.

    The following code example adds a table to the active document. To use this example, run it from the ThisAddIn class in your project.

    Word.Range tableLocation = 
        this.Application.ActiveDocument.Range(0, 0);
    this.Application.ActiveDocument.Tables.Add(
        tableLocation, 3, 4);
    

When you create a table, it is automatically added to the Tables collection of the Document. You can then refer to the table by its item number by using the Item[] property, as shown in the following code.

To refer to a table by item number

  1. Use the Item[] property and supply the item number of the table that you want to refer to.

    The following code example uses the active document. To use this example, run it from the ThisAddIn class in your project.

    Word.Table newTable = this.Application.ActiveDocument.Tables[1];
    

    Each Table object also has a Range property that enables you to set formatting attributes.

To apply a style to a table

  1. Use the Style property to apply one of the Word built-in styles to a table.

    The following code example uses the active document. To use this example, run it from the ThisAddIn class in your project.

    this.Application.ActiveDocument.Tables[1].Range.Font.Size = 8;
    this.Application.ActiveDocument.Tables[1].set_Style("Table Grid 8");