How to: Programmatically Create Word Tables

 

The Tables collection is a member of the Microsoft.Office.Interop.Word.Document, Microsoft.Office.Tools.Word.Document, Selection, and Range classes, which means that you can create a table in any of those contexts. You use the M:Microsoft.Office.Interop.Word.Tables.Add(Microsoft.Office.Interop.Word.Range,System.Int32,System.Int32,System.Object@,System.Object@) 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.

To add a simple table to a document

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

To refer to a table by item number

  1. Use the P:Microsoft.Office.Interop.Word.Tables.Item(System.Int32) 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.

            Dim newTable As Word.Table = Me.Tables.Item(1)
    

Each Table object also has a P:Microsoft.Office.Interop.Word.Table.Range property that enables you to set formatting attributes.

To apply a style to a table

  1. Use the P:Microsoft.Office.Interop.Word.Table.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.

            Me.Tables.Item(1).Range.Font.Size = 8
            Me.Tables.Item(1).Style = "Table Grid 8"
    

To add a simple table to a document

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

To refer to a table by item number

  1. Use the P:Microsoft.Office.Interop.Word.Tables.Item(System.Int32) 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.

            Dim newTable As Word.Table = Me.Application.ActiveDocument.Tables.Item(1)
    

Each Table object also has a P:Microsoft.Office.Interop.Word.Table.Range property that enables you to set formatting attributes.

To apply a style to a table

  1. Use the P:Microsoft.Office.Interop.Word.Table.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.

            Me.Application.ActiveDocument.Tables.Item(1).Range.Font.Size = 8
            Me.Application.ActiveDocument.Tables.Item(1).Style = "Table Grid 8"
    

How to: Programmatically Add Text and Formatting to Cells in Word Tables
How to: Programmatically Add Rows and Columns to Word Tables
How to: Programmatically Populate Word Tables with Document Properties
Optional Parameters in Office Solutions

Show: