Ash
Ash

Reputation: 41

Apply table style to header using VBA

I'm writing VBA code in Word to format a table. I need to allow the user to format the table header independently of the table body. To do this I hoped to be able to use the built in table styles, however, when I do this the style is applied to the whole table and not just the header or body.

Below is the code that I'm using to apply a style to the header:

Sub FormatTableHeader(ByVal control As IRibbonControl)
Dim tbl As Table

If Selection.Information(wdWithInTable) = True Then
    Set tbl = Selection.Tables(1)

    tbl.Rows(1).Range.Style = "TableHeader"
End If

End Sub

I also need to do something similar for the table body.

Is it possible to do what I'm trying to do using the table styles? I know I could write code to format the table header, but I hoped to keep the code linked to the styles.

Upvotes: 0

Views: 1544

Answers (1)

G. Jonathan
G. Jonathan

Reputation: 74

Not sure if this is possible but you could follow this procedure to create your own styles and apply them on the header row or the table body.

Sub tableFormat()
Dim lastRow As Integer

lastRow = ActiveDocument.Tables(1).Rows.Count


With ActiveDocument.Tables(1).Rows(1).Range.Font
        .Name = "Arial"
        .Size = 12
        .Italic = True
        .Bold = True
End With

For i = 2 To lastRow
With ActiveDocument.Tables(1).Rows(i).Range.Font
        .Name = "Arial"
        .Size = 8
        .Italic = False
        .Bold = False
End With
Next i
End Sub

Upvotes: 0

Related Questions