Cell Object

Word Developer Reference

Represents a single table cell. The Cell object is a member of the Cells collection. The Cells collection represents all the cells in the specified object.

Remarks

Use Cell(row, column), where row is the row number and column is the column number, or Cells(index), where index is the index number, to return a Cell object. The following example applies shading to the second cell in the first row.

Visual Basic for Applications
  Set myCell = ActiveDocument.Tables(1).Cell(Row:=1, Column:=2)
myCell.Shading.Texture = wdTexture20Percent

The following example applies shading to the first cell in the first row.

Visual Basic for Applications
  ActiveDocument.Tables(1).Rows(1).Cells(1).Shading _
    .Texture = wdTexture20Percent

Use the Add method to add a Cell object to the Cells collection. You can also use the InsertCells method of the Selection object to insert new cells. The following example adds a cell before the first cell in myTable.

Visual Basic for Applications
  Set myTable = ActiveDocument.Tables(1)
myTable.Range.Cells.Add BeforeCell:=myTable.Cell(1, 1)

The following example sets a range (myRange) that references the first two cells in the first table. After the range is set, the cells are combined by the Merge method.

Visual Basic for Applications
  Set myTable = ActiveDocument.Tables(1)
Set myRange = ActiveDocument.Range(myTable.Cell(1, 1) _
    .Range.Start, myTable.Cell(1, 2).Range.End)
myRange.Cells.Merge

Remarks

Use the Add method with the Rows or Columns collection to add a row or column of cells.

Use the Information property with a Selection object to return the current row and column number. The following example changes the width of the first cell in the selection and then displays the cell's row number and column number.

Visual Basic for Applications
  If Selection.Information(wdWithInTable) = True Then
    With Selection
        .Cells(1).Width = 22
        MsgBox "Cell " & .Information(wdStartOfRangeRowNumber) _
            & "," & .Information(wdStartOfRangeColumnNumber)
    End With
End If

See Also