Class Tables (Word VBA)
A collection of Table objects that represent the tables in a selection, range, or document. To use a Tables class variable it first needs to be instantiated, for example
Dim tbls as Tables
Set tbls = ActiveDocument.Tables
For Each
Here is an example of processing the Tables items in a collection.
Dim tbl As Table
For Each tbl In ActiveDocument.Tables
Next tbl
Add
Returns a Table object that represents a new, blank table added to a document.
Add (Range, NumRows, NumColumns, DefaultTableBehavior, AutoFitBehavior)
Set myRange = ActiveDocument.Range(0, 0)
ActiveDocument.Tables.Add Range:=myRange, NumRows:=3, NumColumns:=4
Arguments
The following arguments are required:
Range (Range) - The range where you want the table to appear. The table replaces the range, if the range isn't collapsed.
NumRows (Long) - The number of rows you want to include in the table.
NumColumns (Long) - The number of columns you want to include in the table.
Optional arguments
The following arguments are optional
DefaultTableBehavior (WdDefaultTableBehavior) - 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.
Possible return values are wdWord8TableBehavior - Disables AutoFit. Default, wdWord9TableBehavior - Enables AutoFit.
AutoFitBehavior (WdAutoFitBehavior) - Sets the AutoFit rules for how Word sizes tables. Can be one of the WdAutoFitBehavior constants.
Possible return values are wdAutoFitContent - The table is automatically sized to fit the content contained in the table, wdAutoFitFixed - The table is set to a fixed size, regardless of the content, and is not automatically sized, wdAutoFitWindow - The table is automatically sized to the width of the active window.
Count
Returns a Long that represents the number of tables in the collection.
Dim lngCount As Long
lngCount = ActiveDocument.Tables.Count
Item
Returns an individual Table object in a collection.
Item (Index)
Index: The individual object to be returned. Can be a Long indicating the ordinal position of the individual object.
Dim tbl As Table
Set tbl = ActiveDocument.Tables(Index:=1)
NestingLevel
Returns the nesting level of the specified tables.
The outermost table has a nesting level of 1. The nesting level of each successively nested table is one higher than the previous table.
Dim lngNestingLevel As Long
lngNestingLevel = ActiveDocument.Tables.NestingLevel