如何解决使用 VBA 在 Word 页脚中格式化第 XX 页的问题
我正在通过代码构建我的页脚。通过 VBA 添加字段时,我在 Word 页脚中正确格式化页数时遇到问题。当代码运行时,格式总是以 XXof XX (1of 20) 而不是 XX of XX (1 of 20) 结束。我尝试了以下方法,但数字总是显示页码,“of”一词前没有空格。
With rng
.Text = "NUMPAGES "
Set oFooterRng1 = rng.Words(1)
.Fields.Add Range:=oFooterRng1,Type:=wdFieldEmpty,Text:="NUMPAGES \* arabic ",PreserveFormatting:=True
End With
rng.Collapse wdCollapseStart
rng.Text = " of "
rng.Collapse wdCollapseStart
With rng
.Text = "PAGE "
Set oFooterRng1 = rng.Words(1)
.Fields.Add Range:=oFooterRng1,Text:="PAGE \* arabic ",PreserveFormatting:=True
End With
或者这个
With rng
.Text = "PAGE of NUMPAGES "
Set oFooterRng1 = rng.Words(1)
Set oFooterRng2 = rng.Words(3)
.Fields.Add Range:=oFooterRng1,PreserveFormatting:=True
.Fields.Add Range:=oFooterRng2,PreserveFormatting:=True
End With
解决方法
尝试基于以下内容:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
.InsertAfter Text:="Page "
.Fields.Add Range:=.Characters.Last,Type:=wdFieldEmpty,Text:="PAGE",PreserveFormatting:=False
.InsertAfter Text:=" of "
.Fields.Add Range:=.Characters.Last,Text:="NUMPAGES",PreserveFormatting:=False
End With
Application.ScreenUpdating = True
End Sub
并且,在您尝试实现的过程中反向执行:
Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
.Fields.Add Range:=.Characters.Last,PreserveFormatting:=False
.Collapse wdCollapseStart
.Text = " of "
.Collapse wdCollapseStart
.Fields.Add Range:=.Duplicate,PreserveFormatting:=False
.Collapse wdCollapseStart
.Text = "Page "
End With
Application.ScreenUpdating = True
End Sub
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。