页面页脚,第Y页的第X页

问题描述

我有以下vba代码

    Sub pageNumber(objOutputDoc As Object)

'    With objOutputDoc.Sections(objOutputDoc.Sections.Count)
'        .Footers(wdHeaderFooterPrimary).Range.Text = vbTab & vbTab & "Page "
'        .Footers(wdHeaderFooterPrimary).PageNumbers.Add FirstPage:=True
'    End With
    Dim i As Long,bAdd As Boolean: bAdd = True
    With objOutputDoc.Sections(objOutputDoc.Sections.Count).Footers(wdHeaderFooterPrimary).Range 'First.Headers(wdHeaderFooterPrimary).Range
      For i = 1 To .Fields.Count
        If .Fields(i).Type = wdFieldPage Then
          bAdd = False: Exit For
        End If
      Next
      If bAdd = True Then
        .InsertAfter vbTab & vbTab & "Page "
        .Fields.Add .Characters.Last,wdFieldEmpty,"PAGE",False
        .InsertAfter " of "
        .Fields.Add .Characters.Last,"NUMPAGES",False
      End If
    End With
End Sub

上面的vba代码自动将Y的第X页添加到我的Word文档中。我的Word文档包含27个页面,所以当我运行vba时,页面编号将插入第1页,共27页,第2页,共27页,依此类推。我遇到的问题是我的最后一页,即第27页,我已经插入在最后一页的底部第1页,共1页。当我运行vba时,现在第27页的第27页插入了最后一页,现在我有第1页的第1页和第27页的27。我希望倒数第二页是Page 26之26。

解决方法

提供的第1至26页位于同一部分,您可以使用SECTIONPAGES而不是NUMPAGES来实现目标。

然后在以下部分中,您需要重新启动页码:

With objOutputDoc.Sections(objOutputDoc.Sections.Count).HeaderFooter.PageNumbers
    .RestartNumberingAtSection = True
    .StartingNumber = 1
End With

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...