Table object

The Table object allow you to work with a table. The Tables collection contains all of the tables in a document.

Creating a table

Use the Add method to add a new table to a document and to the Tables collection. You must specify the location of the table as well as the dimensions of the table.

{Add a table with eight rows and two columns.}
wordTable = document.Tables.Add(wordRange, 8, 2);

Retrieving a table

You can retrieve a table from the Tables collection with the Item method by the number associated with the table.

{Retrieve the first table.}
wordTable = document.Tables.Item(1);

Formatting a table

The Table object contains several methods and properties to modify the appearance of a table, such as the AutoFormat method, the Borders property, the Shading property, and the Spacing property.

AutoFormat method   The AutoFormat method allows you to specify a predefined format for the table. Use a constant contained in the wdTableFormat enumeration to specify the format of the table.

{Format the current table.}
table.AutoFormat(Word.wdTableFormatContemporary);

Borders property   The Borders property returns a Borders collection that contains all of the table borders. You can use the Borders collection to set the properties for the borders, such as width and style.

Use the OutsideLineStyle property or the InsideLineStyle property and a constant contained in the wdLineStyle enumeration to set the style of the borders.

{Set the style of the outside borders of the table.}
wordTable.Borders.OutsideLineStyle = Word.wdLineStyleTriple;

Use the OutsideLineWidth property or the InsideLineWidth property and a constant from the wdLineWidth enumeration to set the width of the borders.

{Set the width of the inside borders of the table to 0.25 points.}
wordTable.Borders.InsideLineWidth = Word.wdLineWidth025pt;

You can also access individual borders in a table with the Borders collection. To specify a border, use the Item method and the constants contained in the wdBorderType enumeration.

{Set the line style for the bottom border of the table.}
wordTable.Borders.Item(Word.wdBorderBottom).LineStyle = Word.wdLineStyleDouble;

Shading property   The Shading property returns a Shading object. The Shading object contains properties, such as the Texture property, that modify the shading of the table. Use the Texture property and a constant contained in the wdTextureIndex enumeration to set the texture of the shading.

{Set the texture of the table’s shading.}
wordTable.Shading.Texture = Word.wdTextureDiagonalCross;

Spacing property   The Spacing property sets the spacing between the cells in the table. You must supply the spacing between the cells in points.

{Set the spacing between the cells in a table to 7 points.}
wordTable.Spacing = 7;


Documentation Feedback