【问题标题】:VBA word, Add Pagenumber to text of footerVBA word,将页码添加到页脚文本
【发布时间】:2019-01-07 21:55:23
【问题描述】:

我有一个需要用 VBA 宏替换的页脚文本,这就是我正在做的事情:

With ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary)
        text = Replace(.Range.text, "SITECODE", ws.Cells(24, 2))
        text = Replace(text, "STREET", ws.Cells(6, 2))
        text = Replace(text, "SITENAME", ws.Cells(18, 2))
        text = Replace(text, "POSTALCODE", ws.Cells(2, 2))
        text = Replace(text, "Page number:", "Page number: " & wdActiveEndPageNumber)
        .Range.text = Replace(text, "CITY", ws.Cells(10, 2))

    End With

这工作除了一行:

 text = Replace(text, "Page number:", "Page number: " & wdActiveEndPageNumber)

页码始终为 3,在第一个编号的页 2 上也是错误的。如何在文本中添加正确的数字。我知道 PageNumbers.Add 存在,但这与我的文档页脚不太一致。

【问题讨论】:

  • 您的问题得到解答了吗?
  • @CindyMeister 找到了自己的解决方案

标签: vba ms-word


【解决方案1】:

您不应尝试使用 页脚(或页眉)中的静态文本在 Word 中为页面编号。页脚中显示的内容在每一页上都会重复 - 这就是页脚的工作方式。使用PAGE域码,会根据实际页面动态更新。

目前尚不完全清楚您正在执行的操作的结果应该是什么样子。但假设页码位于其他信息的末尾:

Dim doc As Word.Document
Dim rng As Word.Range

Set doc = ActiveDocument
Set rng = doc.Sections(1).Footers(wdHeaderFooterPrimary).Range
rng.Text = "Sitecode" & vbCr & "Sitename" & "Street" & vbCr & _
           "City PostalCode " & vbCr & "Page number: "
rng.Collapse wdCollapseEnd
rng.Fields.Add rng, , "Page ", False

【讨论】:

    【解决方案2】:
     For Each wd In .Range.Words
                If StrComp(wd, "PNR", vbTextCompare) = 0 Then
                    Selection.Fields.Add Range:=wd, Type:=wdFieldEmpty, text:="PAGE  \* Arabic ", PreserveFormatting:=True
                End If
            Next wd
    

    【讨论】:

      猜你喜欢
      • 2018-08-17
      • 2019-05-30
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多