Tables.Add method (Word)
Returns a Table object that represents a new, blank table added to a document.
expression.Add (Range, NumRows, NumColumns, DefaultTableBehavior, AutoFitBehavior)
expression Required. A variable that represents a 'Tables' collection.
Name | Required/Optional | Data type | Description |
---|---|---|---|
Range | Required | Range object | The range where you want the table to appear. The table replaces the range, if the range isn't collapsed. |
NumRows | Required | Long | The number of rows you want to include in the table. |
NumColumns | Required | Long | The number of columns you want to include in the table. |
DefaultTableBehavior | Optional | Variant | Sets 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. |
AutoFitBehavior | Optional | Variant | Sets the AutoFit rules for how Word sizes tables. Can be one of the WdAutoFitBehavior constants. |
Table
This example adds a blank table with three rows and four columns at the beginning of the active document.
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
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.
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
Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.