简体   繁体   English

使用 VBA 将表格样式应用于标题

[英]Apply table style to header using VBA

I'm writing VBA code in Word to format a table.我正在 Word 中编写 VBA 代码来格式化表格。 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.我知道我可以编写代码来格式化表头,但我希望将代码链接到样式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM