Working with Tables





Working with Tables

As previously mentioned, 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 auto-fit 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 then 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.

-41. A VSTO Customization That Creates and Populates a Simple Table
private void ThisDocument_Startup(object sender, EventArgs e)
{
  Word.Range r = Range(ref missing, ref missing);
  Word.Table t = r.Tables.Add(r, 5, 5, ref missing, ref missing);

  for (int i = 1; i <= 5; i++)
  {
    for (int j = 1; j <= 5; j++)
    {
      t.Rows[i].Cells[j].Range.Text = String.Format(
        "{0}, {1}", i, j);
    }
  }
}

The Table object's Cell method provides an easier way of getting to a Cell. The Cell method takes an int 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 auto-formatting 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 set_Style method takes an object by reference that is set to the name of a table style as found in the Table AutoFormat dialog. 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.

-42. A VSTO Customization That Creates a Multiplication Table
private void ThisDocument_Startup(object sender, EventArgs e)
{
  Word.Range r = Range(ref missing, ref missing);
  Word.Table t = r.Tables.Add(r, 12, 12, ref missing,
    ref missing);

  for (int i = 1; i <= 12; i++)
  {
    for (int j = 1; j <= 12; j++)
    {
      Word.Cell c = t.Cell(i,j);
      if (i == 1 && j == 1)
      {
        c.Range.Text = "X";
      }
      else if (i == 1)
      {
        c.Range.Text = j.ToString();
      }
      else if (j == 1)
      {
        c.Range.Text = i.ToString();
      }
      else
      {
        int result = i * j;
        c.Range.Text = result.ToString();
      }
    }
  }

  t.Columns.AutoFit();
  object styleString = "Table Classic 2";
  t.set_Style(ref styleString);
  t.ApplyStyleLastRow = false;
  t.ApplyStyleLastColumn = false;
}



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows