Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Word Solutions
Working with Tables
 How to: Add Rows and Columns to Wor...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Microsoft Visual Studio Tools for the Microsoft Office system (version 3.0)
How to: Add Rows and Columns to 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.

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.

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.

To add a row to a table

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

    Visual Basic
    Me.Tables.Item(1).Rows.Add()
    
    
    C#
    object beforeRow = this.Tables[1].Rows[1];
    
    this.Tables[1].Rows.Add(ref beforeRow);
    
    

To add a column to a table

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

    Visual Basic
    Me.Tables.Item(1).Columns.Add(BeforeColumn:=Me.Tables.Item(1).Columns(1))
    Me.Tables.Item(1).Columns.DistributeWidth()
    
    
    C#
    object beforeColumn = this.Tables[1].Columns[1]; 
    
    this.Tables[1].Columns.Add(ref beforeColumn); 
    this.Tables[1].Columns.DistributeWidth();
    
    

The following code examples can be used in an application-level 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.

To add a row to a table

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

    Visual Basic
    Me.Application.ActiveDocument.Tables.Item(1).Rows.Add()
    
    
    C#
    object beforeRow = this.Application.ActiveDocument.Tables[1].Rows[1];
    this.Application.ActiveDocument.Tables[1].Rows.Add(ref beforeRow);
    
    

To add a column to a table

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

    Visual Basic
    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()
    
    
    C#
    object beforeColumn = this.Application.ActiveDocument.Tables[1].Columns[1];
    
    this.Application.ActiveDocument.Tables[1].Columns.Add(ref beforeColumn);
    this.Application.ActiveDocument.Tables[1].Columns.DistributeWidth();
    
    
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker