This documentation is archived and is not being maintained.

Tables.Add Method

Office 2007
Returns a Table object that represents a new, blank table added to a document.

Syntax

expression.Add(Range, NumRows, NumColumns, DefaultTableBehavior, AutoFitBehavior)

expression   Required. A variable that represents a Tables collection.

Parameters

NameRequired/OptionalData TypeDescription
RangeRequiredRange objectThe range where you want the table to appear. The table replaces the range, if the range isn't collapsed.
NumRowsRequiredLongThe number of rows you want to include in the table.
NumColumnsRequiredLongThe number of columns you want to include in the table.
DefaultTableBehaviorOptionalVariantSets a value that specifies whether Microsoft Word automatically resizes cells in tables to fit the cells’ contents (AutoFit). Can be either of the following constants: wdWord8TableBehavior (AutoFit disabled) or wdWord9TableBehavior (AutoFit enabled). The default constant is wdWord8TableBehavior.
AutoFitBehaviorOptionalVariantSets the AutoFit rules for how Word sizes tables. Can be one of the WdAutoFitBehavior constants.

Return Value
Table

Example

This example adds a blank table with three rows and four columns at the beginning of the active document.

Visual Basic for Applications
Set myRange = ActiveDocument.Range(0, 0)
ActiveDocument.Tables.Add Range:=myRange, NumRows:=3, NumColumns:=4

This example adds a new, blank table with six rows and ten columns at the end of the active document

Visual Basic for Applications
Set MyRange = ActiveDocument.Content
MyRange.Collapse Direction:=wdCollapseEnd
ActiveDocument.Tables.Add Range:=MyRange, NumRows:=6, _
    NumColumns:=10

This example adds a table with three rows and five columns to a new document and then inserts data into each cell in the table.

Visual Basic for Applications
Sub NewTable()
    Dim docNew As Document
    Dim tblNew As Table
    Dim intX As Integer
    Dim intY As Integer

    Set docNew = Documents.Add
    Set tblNew = docNew.Tables.Add(Selection.Range, 3, 5)
    With tblNew
    For intX = 1 To 3
        For intY = 1 To 5
            .Cell(intX, intY).Range.InsertAfter "Cell: R" & intX & ", C" & intY
        Next intY
    Next intX
    .Columns.AutoFit
    End With
End Sub



Show: