在 Word 模板中,我有一个仅包含标题行的表格模板,未设置“允许行跨页分隔”并设置“在每页顶部重复作为标题行”:
在 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 2013 & 2016 – Table ‘repeat header row’ not working – IT Support Guides :选择表格(或将光标放入表格第一页上的任何单元格(这是我自己的发现)),然后在
表格工具->布局
功能区 Pane ,取消选择并重新选择重复标题行
。这可行,但我需要以编程方式执行此操作。
How to enable 'Repeat Header Rows' in a Word Table from VBA? :
wd.ActiveDocument.Tables(1).Rows(1).Select wd.Selection.Rows.HeadingFormat = True
这没有任何效果。
最佳答案
这很可能是一个错误。我从 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/