Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath

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.

Listing 8.41. A VSTO Customization That Creates and Populates a Simple Table

Public Class ThisDocument Private Sub ThisDocument_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup Dim r As Word.Range = Range() Dim t As Word.Table = r.Tables.Add(r, 5, 5) Dim i As Integer For i = 1 To 5 Dim j As Integer For j = 1 To 5 t.Rows(i).Cells(j).Range.Text = _ String.Format("{0}, {1}", i, j) Next Next End Sub End Class

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.

Listing 8.42. A VSTO Customization That Creates a Multiplication Table

Public Class ThisDocument Private Sub ThisDocument_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup Dim r As Word.Range = Range() Dim t As Word.Table = r.Tables.Add(r, 12, 12) Dim i As Integer For i = 1 To 12 Dim j As Integer For j = 1 To 12 Dim c As Word.Cell = t.Cell(i, j) If i = 1 And j = 1 Then c.Range.Text = "X" ElseIf i = 1 Then c.Range.Text = j.ToString() ElseIf j = 1 Then c.Range.Text = i.ToString() Else Dim result As Integer = i * j c.Range.Text = result.ToString() End If Next Next t.Columns.AutoFit() t.Style = "Table Classic 2" t.ApplyStyleLastRow = False t.ApplyStyleLastColumn = False End Sub End Class

Категории