This documentation is archived and is not being maintained.

How to: Create Word Tables


Some code examples in this topic use the this or Me keyword or the Globals class in a way that is specific to document-level customizations, or they rely on features of document-level customizations such as host controls. These examples can be compiled only if you have the required applications installed. For more information, see Features Available by Product Combination.

The Tables collection is a member of the Document, Selection and Range objects, 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.

To add a simple 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.

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

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

To refer to a table by item number

  • Use the Item property and supply the item number of the table that you want to refer to:

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

Formatting Tables

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

To apply a style to a table

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

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

See Also