Working with tables

See Also

This topic includes Visual Basic examples related to the following tasks:

Inserting text into a table cell

The following example inserts text into the first cell of the first table in the active document. The Cell method returns a single Cell object. The Range property returns a Range object. The Delete method is used to delete the existing text and the InsertAfter method inserts the "Cell 1,1" text.

If ActiveDocument.Tables.Count >= 1 Then
    With ActiveDocument.Tables(1).Cell(Row:=1, Column:=1).Range
        .Delete
        .InsertAfter Text:="Cell 1,1"
    End With
End If

Creating a table, inserting text, and applying formatting

The following example inserts a 4 column, 3 row table at the beginning of the document. The For Each...Next structure is used to step through each cell in the table. Within the For Each...Next structure, the InsertAfter method is used to add text to the table cells (Cell 1, Cell 2, and so on).

Set oDoc = ActiveDocument
Set oTable = oDoc.Tables.Add( _
    Range:=oDoc.Range(Start:=0, End:=0), NumRows:=3, _
    NumColumns:=4)
iCount = 1
For Each oCell In oTable.Range.Cells
    oCell.Range.InsertAfter "Cell " & iCount
    iCount = iCount + 1
Next oCell
oTable.AutoFormat Format:=wdTableFormatColorful2, _
    ApplyBorders:=True, ApplyFont:=True, ApplyColor:=True

Returning text from a table cell without returning the end of cell marker

The following examples return and display the contents of each cell in the first row of the first document table.

Set oTable = ActiveDocument.Tables(1)
For Each aCell In oTable.Rows(1).Cells
    Set myRange = ActiveDocument.Range(Start:=aCell.Range.Start, _
        End:=aCell.Range.End - 1)
    MsgBox myRange.Text
Next aCell

Set oTable = ActiveDocument.Tables(1)
For Each aCell In oTable.Rows(1).Cells
    Set myRange = aCell.Range
    myRange.MoveEnd Unit:=wdCharacter, Count:=-1
    MsgBox myRange.Text
Next aCell

Converting existing text to a table

The following example inserts tab-delimited text at the beginning of the active document and then converts the text to a table.

Set oRange1 = ActiveDocument.Range(Start:=0, End:=0)
oRange1.InsertBefore "one" & vbTab & "two" & vbTab & "three" & vbCr
Set oTable1 = oRange1.ConvertToTable( _
    Separator:=Chr(9), NumRows:=1, NumColumns:=3)

Returning the contents of each table cell

The following example defines an array equal to the number of cells in the first document table (assuming Option Base 1). The For Each...Next structure is used to return the contents of each table cell and assign the text to the corresponding array element.

If ActiveDocument.Tables.Count >= 1 Then
    Set oTable = ActiveDocument.Tables(1)
    iNumCells = oTable.Range.Cells.Count
    ReDim aCells(iNumCells)
    i = 1
    For Each oCell In oTable.Range.Cells
        Set myRange = oCell.Range
        myRange.MoveEnd Unit:=wdCharacter, Count:=-1
        aCells(i) = myRange.Text
        i = i + 1
    Next oCell
End If

Copying all tables in the active document into a new document

This example copies the tables from the current document into a new document.

If ActiveDocument.Tables.Count >= 1 Then
    Set oDoc1 = ActiveDocument
    Set MyRange = Documents.Add.Range(Start:=0, End:=0)
    For Each oTable In oDoc1.Tables
        oTable.Range.Copy
        With MyRange
            .Paste
            .Collapse Direction:=wdCollapseEnd
            .InsertParagraphAfter
            .Collapse Direction:=wdCollapseEnd
        End With
    Next
End If