【问题标题】:Adding document property field to footer removes existing page numbering将文档属性字段添加到页脚会删除现有的页码
【发布时间】:2022-01-14 07:08:04
【问题描述】:

我想在文档中每一页的页脚中标记一个文档变量字段。我已经弄清楚如何添加该字段。但是,这样做时,页脚中已经存在的页码字段将被禁用,只留下文本,不再充当动态页码。

这是我添加字段的代码,但以某种方式禁用了页码。 (“sectionFooter.Range.Text = sectionFooter.Range.Text +”“”行是我能找到的唯一方法,甚至可以让页码仍然显示):

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

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    如果这是您希望新字段定位的方式,请尝试这样的操作...

    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
    

    【讨论】:

    • 谢谢! “InsertParagraphAfter”是关键!不过,有一件事 - 字体并没有按照提供的方式应用。我添加了另一个带有完整字体处理的答案。
    【解决方案2】:

    Rich 的回答处理了繁重的工作,但对于遇到这种情况的任何人,我想说明我如何能够隔离新段落并应用字体。

    Set rng = sectionFooter.Range
    
                rng.InsertParagraphAfter
                Set rng = sectionFooter.Range
                rng.Collapse wdCollapseEnd
                Set newPP = rng.Paragraphs.Last
                newPP.Range.Font.Size = 8
                newPP.Range.Font.Name = "Arial"
                rng.Fields.Add Range:=newPP.Range, Type:=wdFieldEmpty, Text:="DOCVARIABLE  ndGeneratedStamp", preserveformatting:=False
    

    【讨论】:

      【解决方案3】:

      根据我的测试,我们也可以删除注释代码。

      注意如果我们使用这种方式,我们需要先设置页码,然后运行宏。

      Dim docSection As Word.Section, sectionFooter As Word.HeaderFooter 
      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
      

      【讨论】:

      • 我认为这也可以 :) 但是,它消除了现有的页脚字段。我一直使用的模板有不同的首页页脚,上面有页码,当我运行宏时,它会消失。
      • 所以我们还需要尝试不同的解决方案来满足您的要求。
      猜你喜欢
      • 1970-01-01
      • 2017-05-14
      • 2020-03-03
      • 2012-07-25
      • 1970-01-01
      • 2019-01-07
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多