Error accessing a table row or column

When you try to access an individual row or column in a drawn table, a runtime error may occur if the table is not uniform. For example, the following instruction posts an error if the first table in the active document doesn't have the same number of rows in each column.

ActiveDocument.Tables(1).Rows(1).Borders.Enable = False

You can avoid this error by first selecting the cells in a column or row using the SelectColumn or SelectRow method. After the selection is made, use the Cells property with the Selection object. The following example selects the first row in the first document table. The Cells property is used to access the selected cells (all the cells in the first row) so that borders can be removed.

ActiveDocument.Tables(1).Cell(1, 1).Select
With Selection
    .SelectRow
    .Cells.Borders.Enable = False
End With

The following example selects the first column in the first document table. The For Each...Next loop is used to add text to each cell in the selection (all the cells in the first column).

ActiveDocument.Tables(1).Cell(1, 1).Select
Selection.SelectColumn
i = 1
For Each oCell In Selection.Cells
    oCell.Range.Text = "Cell " & i
    i = i + 1
Next oCell