Format related procedures in class Table (Word VBA)

This page presents procedures from class Table related to the theme Format: ApplyStyleColumnBands, ApplyStyleDirectFormatting, ApplyStyleFirstColumn, ApplyStyleHeadingRows, ApplyStyleLastColumn, ApplyStyleLastRow, ApplyStyleRowBands, AutoFormat, AutoFormatType, Borders, BottomPadding, LeftPadding, PreferredWidth, PreferredWidthType, RightPadding, Spacing, Style, TopPadding and UpdateAutoFormat

ApplyStyleColumnBands

Returns or sets a Boolean that represents whether to apply style bands to the columns in a table if an applied preset table style provides style banding for columns.

ActiveDocument.Tables(1).ApplyStyleColumnBands = True

ApplyStyleDirectFormatting

Applies the specified style but maintains any formatting that a user directly applies.

Syntax : expression.ApplyStyleDirectFormatting (StyleName)

StyleName: The name of the style to apply.

Dim strStyleName As String: strStyleName =  
ActiveDocument.Tables(1).ApplyStyleDirectFormatting StyleName:=strStyleName

ApplyStyleFirstColumn

True for Microsoft Word to apply first-column formatting to the first column of the specified table.

The specified table style must contain first-column formatting to apply this formatting to a table.

Sub TableStyles() 
 With ActiveDocument.Tables(2) 
 .Style = "Table Style 1" 
 .ApplyStyleFirstColumn = False 
 .ApplyStyleHeadingRows = False 
 .ApplyStyleLastColumn = False 
 .ApplyStyleLastRow = False 
 End With 
End Sub

ApplyStyleHeadingRows

True for Microsoft Word to apply heading-row formatting to the first row of the selected table.

The specified table style must contain heading-row formatting to apply this formatting to a table.

Sub TableStyles() 
 With ActiveDocument.Tables(2) 
 .Style = "Table Style 1" 
 .ApplyStyleFirstColumn = False 
 .ApplyStyleHeadingRows = False 
 .ApplyStyleLastColumn = False 
 .ApplyStyleLastRow = False 
 End With 
End Sub

ApplyStyleLastColumn

True for Microsoft Word to apply last-column formatting to the last column of the specified table.

The specified table style must contain last-column formatting to apply this formatting to a table.

Sub TableStyles() 
 With ActiveDocument.Tables(2) 
 .Style = "Table Style 1" 
 .ApplyStyleFirstColumn = False 
 .ApplyStyleHeadingRows = False 
 .ApplyStyleLastColumn = False 
 .ApplyStyleLastRow = False 
 End With 
End Sub

ApplyStyleLastRow

True for Microsoft Word to apply last-row formatting to the last row of the specified table.

The specified table style must contain last-row formatting to apply this formatting to a table.

Sub TableStyles() 
 With ActiveDocument.Tables(2) 
 .Style = "Table Style 1" 
 .ApplyStyleFirstColumn = False 
 .ApplyStyleHeadingRows = False 
 .ApplyStyleLastColumn = False 
 .ApplyStyleLastRow = False 
 End With 
End Sub

ApplyStyleRowBands

Returns or sets a Boolean that represents whether to apply style bands to the rows in a table if an applied preset table style provides style banding for rows.

ActiveDocument.Tables(1).ApplyStyleRowBands = True

AutoFormat

Applies a predefined look to a table.

The arguments for this method correspond to the options in the Table AutoFormat dialog box.

Syntax : expression.AutoFormat (Format, ApplyBorders, ApplyShading, ApplyFont, ApplyColor, ApplyHeadingRows, ApplyLastRow, ApplyFirstColumn, ApplyLastColumn, AutoFit)

Set newDoc = Documents.Add 
Set myTable = newDoc.Tables.Add(Range:=Selection.Range, _ 
 NumRows:=5, NumColumns:=5) 
myTable.AutoFormat Format:=wdTableFormatColorful2

Arguments

The following arguments are optional

Format (WdTableFormatApply) - The format to apply. This parameter can be a WdTableFormat constant, a WdTableFormatApply constant, or a TableStyle object.

Here you can find possible values for WdTableFormatApply.

ApplyBorders (Boolean) - True to apply the border properties of the specified format. The default value is True.

ApplyShading (Boolean) - True to apply the shading properties of the specified format. The default value is True.

ApplyFont (Boolean) - True to apply the font properties of the specified format. The default value is True.

ApplyColor (Boolean) - True to apply the color properties of the specified format. The default value is True.

ApplyHeadingRows (Boolean) - True to apply the heading-row properties of the specified format. The default value is True.

ApplyLastRow (Boolean) - True to apply the last-row properties of the specified format. The default value is False.

ApplyFirstColumn (Boolean) - True to apply the first-column properties of the specified format. The default value is True.

ApplyLastColumn (Boolean) - True to apply the last-column properties of the specified format. The default value is False.

AutoFit (Boolean) - True to decrease the width of the table columns as much as possible without changing the way text wraps in the cells. The default value is True.

AutoFormatType

Returns the type of automatic formatting that's been applied to the specified table.

This property can be one of the WdTableFormat constants. Use the AutoFormat method to apply automatic formatting to a table.

If ActiveDocument.Tables.Count >= 1 Then 
 If ActiveDocument.Tables(1).AutoFormatType <= wdTableFormatSimple3 Then 
 ActiveDocument.Tables(1).AutoFormat _ 
 Format:=wdTableFormatClassic1 
 End If 
End If

Borders

Returns a Borders collection that represents all the borders for the specified object.

For information about returning a single member of a collection, see Returning an object from a collection.

Set myTable = ActiveDocument.Tables(1) 
With myTable.Borders 
 .InsideLineStyle = wdLineStyleSingle 
 .OutsideLineStyle = wdLineStyleDouble 
End With

BottomPadding

Returns or sets the amount of space (in points) to add below the contents of a single cell or all the cells in a table.

The setting of the BottomPadding property for a single cell overrides the setting of the BottomPadding property for the entire table.

ActiveDocument.Tables(1).BottomPadding = _ 
 PixelsToPoints(40, True)

LeftPadding

Returns or sets the amount of space (in points) to add to the left of the contents of all the cells in a table.

The setting of the LeftPadding property for a single cell overrides the setting of the LeftPadding property for the entire table.

ActiveDocument.Tables(1).LeftPadding = _ 
 PixelsToPoints(40, False)

PreferredWidth

Returns or sets the preferred width (in points or as a percentage of the window width) for the specified table.

If the PreferredWidthType property is set to wdPreferredWidthPoints, the PreferredWidth property returns or sets the width in points. If the PreferredWidthType property is set to wdPreferredWidthPercent, the PreferredWidth property returns or sets the width as a percentage of the window width.

With ActiveDocument.Tables(1) 
 .PreferredWidthType = wdPreferredWidthPercent 
 .PreferredWidth = 50 
End With

PreferredWidthType

Returns or sets the preferred unit of measurement to use for the width of the specified table.

Possible Values are wdPreferredWidthAuto - Automatically select the unit of measure to use based on the current selection, wdPreferredWidthPercent - Measure the current item width using a specified percentage, wdPreferredWidthPoints - Measure the current item width using a specified number of points.
With ActiveDocument.Tables(1) 
 .PreferredWidthType = wdPreferredWidthPercent 
 .PreferredWidth = 50 
End With

RightPadding

Returns or sets the amount of space (in points) to add to the right of the contents of all the cells in a table.

The setting of the RightPadding property for a single cell overrides the setting of the RightPadding property for the entire table.

ActiveDocument.Tables(1).RightPadding = _ 
 PixelsToPoints(40, False)

Spacing

Returns or sets the spacing (in points) between the cells in a table.

ActiveDocument.Tables(1).Spacing = 9

Style

Returns or sets the style for the specified table. Read/write Variant.

To set this property, you can specify the local name of the style, an integer, a WdBuiltinStyle constant, or a TableStyle object that contains the desired formatting for the table.

ActiveDocument.Tables(1).Style =

TopPadding

Returns or sets the amount of space (in points) to add above the contents of all the cells in a table.

The setting of the TopPadding property for a single cell overrides the setting of the TopPadding property for the entire table.

ActiveDocument.Tables(1).TopPadding = _ 
 PixelsToPoints(40, True)

UpdateAutoFormat

Updates the table with the characteristics of a predefined table format.

As an example of how this method functions, if you apply a table format with AutoFormat and then insert rows and columns, the table may no longer match the predefined look. UpdateAutoFormat restores the format.

Dim docNew As Document 
Dim tableNew As Table 
 
Set docNew = Documents.Add 
Set tableNew = docNew.Tables.Add(Selection.Range, 5, 5) 
 
With tableNew 
 .AutoFormat Format:=wdTableFormatColumns1 
 .Rows.Add BeforeRow:=tableNew.Rows(1) 
End With 
MsgBox "Click OK to reapply autoformatting." 
tableNew.UpdateAutoFormat