MS Word-VBA-将文档属性字段添加到页脚中会删除现有页码 [英] MS Word - VBA - Adding document property field to footer removes existing page numbering
问题描述
我想在文档中每个页面的页脚中标记文档变量字段.我已经弄清楚了如何添加字段.但是,这样做时,页脚中已经存在的页码字段将被禁用,仅保留文本,而不再充当动态页码.任何帮助将不胜感激.
I am tying to stamp a document variable field in the footer of every page in a document. I have figured out how to add the field. However, in doing so, the page numbering field that already exists in the footer becomes disabled, leaving only the text and no longer acting as a dynamic page number. Any help would be appreciated.
这是我的代码,该代码添加了该字段,但是以某种方式禁用了页码. ("sectionFooter.Range.Text = sectionFooter.Range.Text +"行是我什至可以找到的页码仍然显示的唯一方法):
Here is my code that adds the field, but disables the page numbers somehow. (the "sectionFooter.Range.Text = sectionFooter.Range.Text + " "" line is the only way I could find to even get the page number to still show):
For Each docSection In ActiveDocument.Sections
For Each sectionFooter In docSection.Footers
sectionFooter.Range.Collapse wdCollapseEnd
sectionFooter.Range.Text = sectionFooter.Range.Text + " "
sectionFooter.Range.Collapse wdCollapseEnd
Dim newPP As Paragraph
Set newPP = sectionFooter.Range.Paragraphs.Add()
newPP.Range.Font.Size = 8
newPP.Range.Font.Name = "Arial"
ActiveDocument.Fields.Add Range:=newPP.Range, Type:=wdFieldEmpty, Text:="DOCVARIABLE ndGeneratedStamp", preserveformatting:=False
Next
Next
推荐答案
如果这是您要放置新字段的方式,请尝试执行以下操作...
If that is how you want the new field positioned, then try something like this ...
Dim docSection As word.Section, sectionFooter As word.HeaderFooter
Dim rng As word.Range
For Each docSection In ActiveDocument.Sections
For Each sectionFooter In docSection.Footers
Set rng = sectionFooter.Range
rng.InsertParagraphAfter
rng.Collapse wdCollapseEnd
rng.Font.Size = 8
rng.Font.Name = "Arial"
rng.Fields.Add Range:=rng, Type:=wdFieldEmpty, Text:="DOCVARIABLE ndGeneratedStamp", PreserveFormatting:=False
Next
Next
这篇关于MS Word-VBA-将文档属性字段添加到页脚中会删除现有页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!