我正在尝试根据 Word 宏中的当前页码处理标题:
Sub InsertHeader()
    Dim oShape As Shape
    Dim PageNumber As Integer
    Dim oSection As Section
    Dim oHeader As HeaderFooter
    For Each oSection In ActiveDocument.Sections
        If oSection.Index > 1 Then
        For Each oHeader In oSection.Headers
            oHeader.Range.Select
            PageNumber = Selection.Information(wdActiveEndPageNumber)
            If oHeader.Exists Then
                Select Case oHeader.Index
                Case Is = wdHeaderFooterFirstPage
                    If PageNumber Mod 2 = 0 Then
                        ActiveDocument.AttachedTemplate.AutoTextEntries("HeaderFirst"). _
    Insert Where:=Selection.Range
                    oHeader.Range.Select
                    Selection.Range.ShapeRange.Left = CentimetersToPoints(2.26)
                    End If
                    If PageNumber Mod 2 = 1 Then
                        ActiveDocument.AttachedTemplate.AutoTextEntries("HeaderFirst"). _
    Insert Where:=Selection.Range
                    End If
                End Select
            End If
        Next oHeader
        End If
    Next oSection
End Sub
当我在本地执行此操作时,宏工作正常。但是,当我在从网络驱动器打开的文档上运行相同的宏时,宏会失败,因为PageNumber它的值是 -1。为什么会这样?
编辑:我不得不重新访问这个宏,在另一台机器上测试表明它也没有在那里运行。雪上加霜的是,宏将不再在我原来的测试环境中运行。
最后,我通过更改页面布局解决了这个问题,这样我就不再需要这个宏了(我使用边距设置中的“装订线”设置做了一个对称的页面布局)。
尽管如此,问题仍然存在:它看起来wdActiveEndPageNumber并不可靠。也许这与从标头调用此函数有关?