【问题标题】:How to Set Table VAlign and First Row Bolding in Word VBA Style?如何在 Word VBA 样式中设置表格 VAlign 和第一行粗体?
【发布时间】:2016-05-12 04:50:20
【问题描述】:

我有一个基本的TableStyle 布局如下:

Sub NewTableStyle()
 Dim StyTbl As Style

 Set StyTbl = ActiveDocument.Styles.Add(Name:="PhaseTable", Type:=wdStyleTypeTable)

 With StyTbl.Table
    .Alignment = wdAlignRowLeft

    With .Condition(wdFirstRow)
        With .Borders(wdBorderBottom)
            .LineStyle = wdLineStyleSingle
            .Visible = True
            .LineWidth = wdLineWidth150pt
        End With
        With .Borders(wdBorderTop)
            .LineStyle = wdLineStyleSingle
            .Visible = True
            .LineWidth = wdLineWidth150pt
        End With
    End With

 End With
End Sub

但我想扩展它,将以下内容应用于所有单元格:

  • 字体:“Times New Roman”,12 号。
  • 垂直对齐:中间

...仅第一行以下内容:

  • 粗体

我想我可以添加如下内容:

 With StyTbl.Font
    .Size = 12
    .Name = "Times New Roman"
 End With

在我的子程序结束时将以下属性应用于整个表。但是,我似乎无法弄清楚选择性的第一行粗体或垂直对齐方式。

我知道这可能是可能的,因为我可以修改 Excel 默认样式以在 GUI 中包含这些选项。如何在我计划应用于表格的 VBA Style 中设置这些选项?

【问题讨论】:

  • 如果你想将样式应用到第一行,只需在 With .Condition(wdFirstRow) ... End With 块内进行。
  • 对,我上面的例子使用了这个......问题是我没有看到任何StyTbl.Table.Condition(wdFirstRow) 的成员导致.Font.bold。你知道如何调用它来到达.Font吗?或者你是说也许我不需要?你是在建议我做类似的事情:StyTbl.Table.Condition(wdFirstRow) StyTable.Font.Bold = True End With?现在试试……
  • 编辑:如果我设置StyTbl.Table.Condition(wdFirstRow) StyTable.Font.Bold = True End With,则将粗体应用于整个表格,而不仅仅是第一行。但是,如果我设置 StyTbl.Table.Condition(wdFirstRow).Font.Bold = True 确实有效...您可以将其作为解决方案发布吗?出于某种原因,StyTbl.Table.Condition(wdFirstRow) 最初并未显示其成员,所以我错过了一些明显的修复。

标签: vba formatting ms-word


【解决方案1】:

您可以访问.Condition(wdFirstRow).Font 来设置您需要的不同设置:

Sub NewTableStyle()
    Dim StyTbl As Style

    Set StyTbl = ActiveDocument.Styles.Add(Name:="PhaseTable", Type:=wdStyleTypeTable)

    With StyTbl.Table
        .Alignment = wdAlignRowLeft

        With .Condition(wdFirstRow)
            With .Borders(wdBorderBottom)
                .LineStyle = wdLineStyleSingle
                .Visible = True
                .LineWidth = wdLineWidth150pt
            End With
            With .Borders(wdBorderTop)
                .LineStyle = wdLineStyleSingle
                .Visible = True
                .LineWidth = wdLineWidth150pt
            End With
            With .Font
                .Bold = False
                .Size = 12
                .Name = "Times New Roman"
            End With

        End With
    End With
End Sub

但似乎没有 vba 访问垂直对齐。

【讨论】:

  • 对...垂直对齐是我不确定的最后一部分...如果没有人想出一种将其设置为样式的方法,或者如果您修改答案以包含他们的解决方案,我会接受这个答案...感谢您的帮助和建议!
  • @JasonR.Mick 它似乎无法通过对象模型获得,但可以在通过 Word UI 创建样式时完成。您是否有机会使用已经包含该样式的 Word 模板(dotx 或 dotm)?从模板创建一个新文档,样式将自动“继承”。
  • 嗨 Cindy,我目前的解决方法如下:Sub AlignVertical(tblNew As Table, rows As Integer) Dim rowIdx As Integer For rowIdx = 1 To rows tblNew.rows(rowIdx).Cells.VerticalAlignment = wdCellAlignVerticalCenter Next rowIdx End Sub 它不像我想要的那样整齐/均匀,但它确实有效。我对桌子高度使用了类似的函数:Sub SetHeight(tblNew As Table, h As Single, rows As Integer) Dim rowIdx As Integer For rowIdx = 1 To rows tblNew.rows(rowIdx).SetHeight h, wdRowHeightExactly Next rowIdx End Sub 似乎是最好的选择。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-06
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-25
相关资源
最近更新 更多