vba - 如果使用 VBA 填充表格,标题行不会自动在后续页面上重复

标签 vba ms-word

在 Word 模板中,我有一个仅包含标题行的表格模板,未设置“允许行跨页分隔”并设置“在每页顶部重复作为标题行”:

table row settings

在 VBA 脚本中(如果重要的话,通过 Word.Application 来自 Excel),我添加了许多数据行,如下所示,以便表格变得比页面大:

Dim table as Word.table
Set table = <get the table>
Dim data()
data = Array("a", "b", "c", "d", "e")

Dim r As Word.row, i, j As Long
For j = 1 to 100
    Set r = table.Rows.Add()
    For i = 0 To UBound(data)
        r.Cells.Item(i + 1).Range.Text = data(i)
    Next
Next

尽管进行了设置,数据行显示正常,但标题行不会在后续页面上重复。


网上找到的解决方案如下:

最佳答案

这很可能是一个错误。我从 Word 2007 一直到 2016 年都看到过它。

这是我找到的解决方法:

'Headings do not start replicating themselves on further pages automatically
'even though they should.
'Have to force their reformat manually.
Application.ScreenUpdating = False
table.Rows(1).HeadingFormat = False
Application.ScreenUpdating = True
table.Rows(1).HeadingFormat = True

所有操作都是必需的,顺序也是如此。否则没有效果。

关于vba - 如果使用 VBA 填充表格,标题行不会自动在后续页面上重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51215557/

相关文章:

vba - Excel VBA 选择使用 Find() 找到的单元格地址

vba - MS Access Control Property.Type 没有意义

matlab - 如何复制 MATLAB 图形并将其粘贴到 Word 文档中?

excel - 从单元格值填充用户窗体上的组合框

asp.net - 在 Internet Explorer 中打开文档时如何将 MS Word 模板文件 (dotx) 保存为 docx

vba - ADODB 记录集记录计数始终返回 -1

vba - 将 Outlook 电子邮件另存为 PDF + 附件

复制下一列的VBA

c# - 将word文档嵌入到另一个没有图标

c# - 在c#中从word文档中获取替换普通文本