【发布时间】:2019-08-26 21:50:18
【问题描述】:
我有一个 word 模板,它根据部分使用不同的页脚字段。有时,此模板的用户会弄乱页脚,因此我正在编写一个宏来通过放回默认页脚字段来修复页脚。
页脚字段根据部分有一些字段逻辑,基本上我需要执行以下操作:
从第 5 节重新开始页码
根据以下部分将文本插入第 1 行第 2 列页脚的表格中
第 1 至 4 节: { PAGE } //注意这是罗马数字格式,页脚设置了“不同的第一页”选项
第 5 节以后 { if { page }
我已经成功完成了第一步并为第 1 到 4 节插入了字段,但是我正在努力解决如何以编程方式将第 5+ 节的复杂字段逻辑插入到我的模板中的相关页脚中VBA? 我需要的代码在下面的代码块中注释为: '这里需要代码才能将以下字段逻辑插入到页脚中
Sub FixPageNumbering()
Dim intSect As Integer
On Error Resume Next
'Insert footer code for Sections 1-4 into row1,col1 of 2x2 table
For intSect = 1 To 4
With ActiveDocument.Sections(intSect).Footers(wdHeaderFooterPrimary)
.PageNumbers.NumberStyle = wdPageNumberStyleLowercaseRoman
.Range.Tables(1).Rows(1).Cells(2).Select
Selection.TypeText Text:="Page "
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"PAGE ", PreserveFormatting:=True
End With
Next intSect
'Set page numbering to restart at #1 from Section 5
With ActiveDocument.Sections(5).Footers(wdHeaderFooterPrimary).PageNumbers
.RestartNumberingAtSection = True
.StartingNumber = 1
End With
'Insert footer code for Sections 5 and onwards into row1,col1 of 2x2 table
For intSect = 5 To ActiveDocument.Sections.Count
With ActiveDocument.Sections(intSect).Footers(wdHeaderFooterPrimary)
.PageNumbers.NumberStyle = wdPageNumberStyleArabic
.Range.Tables(1).Rows(1).Cells(2).Select
'NEED CODE HERE TO INSERT THE FOLLOWING FIELD LOGIC INTO FOOTER
'{ if { page } < { = { pageref ReferencesEnd } + 1 } "Page { = { page } } of { = { pageref ReferencesEnd }" "{Styleref "Att-Appendix Heading" \n }"
End With
Next intSect
ActiveWindow.View.Type = wdPrintView
End Sub
对于第 5 节及以后的部分,页脚字段应显示 & 的第 # 页,或者当有附录时(对于 ReferencesEnd 书签之后存在的页面),它将显示“附录 #”
【问题讨论】:
-
你在这里问什么?您是否要求所有域代码逻辑,正如问题似乎暗示的那样?或者只是为了代码的注释部分?如果是后者,请参阅stackoverflow.com/q/49804968/3077495。但是您可能需要考虑将整个域代码作为 Building Block 存储在模板中,然后只需根据需要插入 Building Block。
-
嗨 Cindy - 澄清一下,我想问一下我需要在上面的代码中添加哪些额外代码来替换我评论为的位:“需要在此处插入以下代码” FIELD LOGIC INTO FOOTER' 我看过你链接到的文章,虽然它看起来可以提供一个解决方案,但我很难弄清楚我将在哪里开始使用它来插入我的字段逻辑,即 { if { page }
标签: vba ms-word footer page-numbering