Dela via


How to: Programmatically add rows and columns to Word tables

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

In a Microsoft Office Word table, the cells are organized into rows and columns. You can use the Add method of the Rows object to add rows to the table and the Add method of the Columns object to add columns.

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.

Document-level customization examples

The following code examples can be used in a document-level customization. To use these examples, run them from the ThisDocument class in your project. These examples assume that the document associated with your customization already has at least one table.

Important

This code runs only in projects that you create by using any of the following project templates:

To add a row to a table

  1. Use the Add method to add a row to the table.

    Me.Tables.Item(1).Rows.Add()
    
    this.Tables[1].Rows.Add(this.Tables[1].Rows[1]);
    

To add a column to a table

  1. Use the Add method, and then use the DistributeWidth method to make all the columns the same width.

    Me.Tables.Item(1).Columns.Add(BeforeColumn:=Me.Tables.Item(1).Columns(1))
    Me.Tables.Item(1).Columns.DistributeWidth()
    
    this.Tables[1].Columns.Add(this.Tables[1].Columns[1]); 
    this.Tables[1].Columns.DistributeWidth();
    

VSTO Add-in examples

The following code examples can be used in a VSTO Add-in. To use the examples, run them from the ThisAddIn class in your project. These examples assume that the active document already has at least one table.

Important

This code runs only in projects that you create by using Word VSTO Add-in templates.

If you want to perform this task in any other type of project, you must add a reference to the Microsoft.Office.Interop.Word assembly, and then you must use classes from that assembly to add rows and columns to tables. For more information, see How to: Target Office applications through primary interop assemblies and Word 2010 primary interop assembly reference.

To add a row to a table

  1. Use the Add method to add a row to the table.

    Me.Application.ActiveDocument.Tables.Item(1).Rows.Add()
    
    this.Application.ActiveDocument.Tables[1].Rows.Add(
        this.Application.ActiveDocument.Tables[1].Rows[1]);
    

To add a column to a table

  1. Use the Add method, and then use the DistributeWidth method to make all the columns the same width.

    Me.Application.ActiveDocument.Tables.Item(1).Columns.Add( _
        BeforeColumn:=Me.Application.ActiveDocument.Tables.Item(1).Columns(1))
    Me.Application.ActiveDocument.Tables.Item(1).Columns.DistributeWidth()
    
    this.Application.ActiveDocument.Tables[1].Columns.Add(
        this.Application.ActiveDocument.Tables[1].Columns[1]);
    this.Application.ActiveDocument.Tables[1].Columns.DistributeWidth();
    

See also