TableStyle Object

Microsoft Word Visual Basic

TableStyle Object

         
Style TableStyle
Multiple objects

Represents a single style that can be applied to a table.

Using the TableStyle object

Use the Table property of the Styles object to return a TableStyle object. Use the Borders property to apply borders to an entire table. Use the Condition method to apply borders or shading only to specified sections of a table. This example creates a new table style and formats the table with a surrounding border. Special borders and shading are applied to the first and last rows and the last column.

Sub NewTableStyle()
    Dim styTable As Style

    Set styTable = ActiveDocument.Styles.Add( _
        Name:="TableStyle 1", Type:=wdStyleTypeTable)

    With styTable.Table

        'Apply borders around table
        .Borders(wdBorderTop).LineStyle = wdLineStyleSingle
        .Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
        .Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
        .Borders(wdBorderRight).LineStyle = wdLineStyleSingle

        'Apply a double border to the heading row
        .Condition(wdFirstRow).Borders(wdBorderBottom) _
            .LineStyle = wdLineStyleDouble

        'Apply a double border to the last column
        .Condition(wdLastColumn).Borders(wdBorderLeft) _
            .LineStyle = wdLineStyleDouble

        'Apply shading to last row
        .Condition(wdLastRow).Shading _
            .BackgroundPatternColor = wdColorGray125

    End With

End Sub