Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Word Solutions
Working with Tables
 How to: Create Word Tables
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: Create Word Tables

Updated: November 2007

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.

The Tables collection is a member of the 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 Add(Range, Int32, Int32, Object%, Object%) method of the Tables collection to add a table at the specified range.

To add a simple table to a document

  • Use the Add(Range, Int32, Int32, Object%, Object%) 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.

    Visual Basic
    Dim tableLocation As Word.Range = Me.Range(Start:=0, End:=0)
    Me.Tables.Add(Range:=tableLocation, NumRows:=3, NumColumns:=4)
    
    
    C#
    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 host item. You can then refer to the table by its item number by using the Item(Int32) property, as shown in the following code.

To refer to a table by item number

  • Use the Item(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.

    Visual Basic
    Dim newTable As Word.Table = Me.Tables.Item(1)
    
    
    C#
    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

  • 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.

    Visual Basic
    Me.Tables.Item(1).Range.Font.Size = 8
    Me.Tables.Item(1).Style = "Table Grid 8"
    
    
    C#
    object styleName = "Table Grid 8";
    
    this.Tables[1].Range.Font.Size = 8;
    this.Tables[1].set_Style(ref styleName);
    
    

To add a simple table to a document

  • Use the Add(Range, Int32, Int32, Object%, Object%) 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.

    Visual Basic
    Dim tableLocation As Word.Range = Me.Application.ActiveDocument.Range(Start:=0, End:=0)
    Me.Application.ActiveDocument.Tables.Add(Range:=tableLocation, NumRows:=3, NumColumns:=4)
    
    
    C#
    object start = 0;
    object end = 0;
    
    Word.Range tableLocation = 
        this.Application.ActiveDocument.Range(ref start, ref end);
    this.Application.ActiveDocument.Tables.Add(
        tableLocation, 3, 4, ref missing, ref missing);
    
    

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(Int32) property, as shown in the following code.

To refer to a table by item number

  • Use the Item(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.

    Visual Basic
    Dim newTable As Word.Table = Me.Application.ActiveDocument.Tables.Item(1)
    
    
    C#
    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

  • 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.

    Visual Basic
    Me.Application.ActiveDocument.Tables.Item(1).Range.Font.Size = 8
    Me.Application.ActiveDocument.Tables.Item(1).Style = "Table Grid 8"
    
    
    C#
    object styleName = "Table Grid 8";
    
    this.Application.ActiveDocument.Tables[1].Range.Font.Size = 8;
    this.Application.ActiveDocument.Tables[1].set_Style(ref styleName);
    
    
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker