在Word文档中配置页脚(页码和文本)
我正在尝试从 Excel 中建立一个 Word 文档,实际上效果很好。然而,页脚的配置在某种程度上是一个谜。
页脚应包含日期(来自变量 VDate
的字符串)、垂直线 |
和 Page X of Y
。这是我到目前为止编写的代码:
...
VDate = "31.03.2022" 'String
Set WordRange = objDoc.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
WordRange.Delete
With WordRange
.Text = VDate & " | Page "
.Collapse wdCollapseEnd
.Fields.Add WordRange, wdFieldPage '<-- works until here
.Collapse wdCollapseEnd
.InsertAfter " of "
.Fields.Add WordRange, wdFieldNumPages
End With
...
添加第一个字段 wdFieldPage
后,第二个 Collapse 不起作用,单词 of
直接插入到 Page
之后> 第二个字段 wdFieldNumPages
直接位于第一个字段之后,导致:
"31.03.2022 | Page of 11"
有人能帮忙吗?非常感谢!
I'm trying to set up a Word document out of Excel what actually works pretty well. However, the configuration of the footer is somehow a riddle.
The footer shall contain a date (String from variable VDate
), a vertical line |
and a Page X of Y
. Here's what I've coded so far:
...
VDate = "31.03.2022" 'String
Set WordRange = objDoc.Sections(1).Footers(WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
WordRange.Delete
With WordRange
.Text = VDate & " | Page "
.Collapse wdCollapseEnd
.Fields.Add WordRange, wdFieldPage '<-- works until here
.Collapse wdCollapseEnd
.InsertAfter " of "
.Fields.Add WordRange, wdFieldNumPages
End With
...
After adding the first field wdFieldPage
the second Collapse does not work and the word of
is inserted directly after Page
and the second field wdFieldNumPages
comes directly after the first field, leading to this:
"31.03.2022 | Page of 11"
Can anybody help? Many thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
.Collapse wdCollapseEnd
工作之后放置.MoveEnd
:Put a
.MoveEnd
after.Collapse wdCollapseEnd
works: