July 3, 2011, 2:51 a.m.
posted by ska
Working with Tables
As mentioned earlier, both the Document and Range object have a Tables property that returns the Tables collection, which contains tables in the Document or Range. To add a Table, you can use the Tables collection's Add method, which takes a Range where you want to add the table, the number of rows and number of columns in the table, and two optional Object parameters passed by reference that specify the autofit behavior of the table. The Add method returns the newly added table.
Listing 8.41 shows code that adds and populates a small table. It uses the returned Table object's Rows property to get the Rows collection. It uses the index operator () on the Rows collection to get an individual Row object. It uses the Row object's Cells property to get the Cells collection. It uses the index operator on the Cells collection to get to an individual Cell object. Finally, it uses the Cell object's Range property to get a Range corresponding to the Cell object and uses the Range object's Text to property set the value of the cell.
A VSTO Customization That Creates and Populates a Simple Table
The Table object's Cell method provides an easier way of getting to a Cell. The Cell method takes an Integer row-and-column parameter and returns a Cell object. Listing 8.42 shows the use of the Cell method, along with the use of several autoformatting techniques as we create a simple multiplication table. The Columns object's AutoFit method is used to resize the column widths to fit the contents of the cells. The Table object's Style property takes an Object by reference that is set to the name of a table style as found in the Table AutoFormat dialog box. The Table object's ApplyStyleLastRow and ApplyStyleLastColumn properties are set to False in Listing 8.42 to specify that no special style be applied to the last row or last column in the table.
A VSTO Customization That Creates a Multiplication Table