Class Table (Word VBA)

The class Table represents a single table. The Table object is a member of the Tables collection. The Tables collection includes all the tables in the specified selection, range, or document.

The main procedures of class Table are Delete, Select and Tables.Add

Set

To use a Table class variable it first needs to be instantiated, for example

Dim tbl as Table
Set tbl = ActiveDocument.Tables(Index:=1)

The following procedures can be used to set variables of type Table: Range.ConvertToTable, Selection.ConvertToTable, Split, Tables.Add, Tables.Item, Cell.Tables, Document.Tables, Range.Tables, Range.TopLevelTables, Selection.Tables, Selection.TopLevelTables and Tables

For Each

Here is an example of processing the Table items in a collection.

Dim tbl As Table
For Each tbl In ActiveDocument.Tables
	
Next tbl

Themes

Some procedures in this class have been grouped together in themes and are described on separate theme pages

Format with its procedures ApplyStyleColumnBands, ApplyStyleDirectFormatting, ApplyStyleFirstColumn, ApplyStyleHeadingRows, ApplyStyleLastColumn, ApplyStyleLastRow, ApplyStyleRowBands, AutoFormat, AutoFormatType, Borders, BottomPadding, LeftPadding, PreferredWidth, PreferredWidthType, RightPadding, Spacing, Style, TopPadding and UpdateAutoFormat
Sort/Order with its procedures Sort, SortAscending and SortDescending

Methods

These are the main methods of the Table class

Delete - Deletes the specified table.

ActiveDocument.Tables(1).Delete

Select - Selects the specified table.

ActiveDocument.Tables(1).Select

Tables.Add - Returns a Table object that represents a new, blank table added to a document.

Dim lngNumRows As Long: lngNumRows = 
Dim lngNumColumns As Long: lngNumColumns =  
Dim tbl As Table
Set tbl = ActiveDocument.Tables.Add(Range:=, NumRows:=lngNumRows, NumColumns:=lngNumColumns)

Other Methods

Cell - Returns a Cell object that represents a cell in a table.

Dim lngRow As Long: lngRow = 
Dim lngColumn As Long: lngColumn =  
Dim cll As Cell
Set cll = ActiveDocument.Tables(1).Cell(Row:=lngRow, Column:=lngColumn)

ConvertToText - Converts a table to text and returns a Range object that represents the delimited text.

Dim rngConvertToText As Range
Set rngConvertToText = ActiveDocument.Tables(1).ConvertToText()

Properties

AllowAutoFit allows Microsoft Word to automatically resize cells in a table to fit their contents.

ActiveDocument.Tables(1).AllowAutoFit = True

Columns returns a Columns collection that represents all the table columns in the table.

Dim clmsColumns As Columns
Set clmsColumns = ActiveDocument.Tables(1).Columns

Descr returns or sets a String that contains a description for the specified table.

ActiveDocument.Tables(1).Descr =

ID returns or sets the identifying label for the specified table when the document is saved as a webpage.

ActiveDocument.Tables(1).ID =

NestingLevel returns the nesting level of the specified table.

Dim lngNestingLevel As Long
lngNestingLevel = ActiveDocument.Tables(1).NestingLevel

Parent returns an object that represents the parent object of the specified Table object.

Dim objParent As Object
Set objParent = ActiveDocument.Tables(1).Parent

Range returns a Range object that represents the portion of a document that is contained within the specified table.

Dim rngRange As Range
Set rngRange = ActiveDocument.Tables(1).Range

Rows returns a Rows collection that represents all the table rows within a table.

Dim rwsRows As Rows
Set rwsRows = ActiveDocument.Tables(1).Rows

Shading returns a Shading object that refers to the shading formatting for the specified object.

Dim shdShading As Shading
Set shdShading = ActiveDocument.Tables(1).Shading

TableDirection returns or sets the direction in which Microsoft Word orders cells in the specified table.

ActiveDocument.Tables(1).TableDirection = wdTableDirectionLtr

Tables returns a Tables collection that represents all the tables nested within the specified table.

Dim tblsTables As Tables
Set tblsTables = ActiveDocument.Tables(1).Tables

Title returns or sets a String that contains a title for the specified table.

ActiveDocument.Tables(1).Title =

Uniform true if all the rows in a table have the same number of columns.

Dim booUniform As Boolean
booUniform = ActiveDocument.Tables(1).Uniform

Tables.Count returns a Long that represents the number of tables in the collection.

Dim lngCount As Long
lngCount = ActiveDocument.Tables.Count

Tables.NestingLevel returns the nesting level of the specified tables.

Dim lngNestingLevel As Long
lngNestingLevel = ActiveDocument.Tables.NestingLevel

Tables.Parent returns an object that represents the parent object of the specified Tables object.

Dim objParent As Object
Set objParent = ActiveDocument.Tables.Parent