Word VBA获取部分中特定页脚的页码

时间:2018-04-02 09:14:51

标签: vba ms-word word-vba

找不到我要找的答案。

我想获取当前页码字符串,包括其格式 例如:某些部分可能有章节标识符(1-1),有些部分是罗马风格等。

我希望能够选择特定的页脚,然后遍历字段并获取Page字段数据(Output是我想要的字符串)。

据我所知,没有选项可以遍历给定部分的页脚,只需获取常规模板并尝试使用它。
我知道来自Selection.Range.Information的{​​{1}},但它只是给了我部分信息。

我错了吗?有没有办法使用我选择的特定页脚?
如果没有,你能指导我如何获得以下数据:

  • 最近的章节编号值
  • 获取特殊格式的页码值,例如罗马字母,字母字体(在wdActiveEndAdjustedPageNumber上应用页面格式的含义)

感谢。

编辑以澄清:

  1. 在我的单词模板中,标题1样式会创建以下标题:第1章,然后是第2章,依此类推。
    在页码格式中,可以选择将当前章节值包含在页码中 例如:假设以下设置

    Page Number format
    将在{ PAGE }字段中显示这些页面:1-1,1-2,1-3,... 我的目标是以某种方式获得特定页面页脚的整个“价值” 这是不会正常工作的代码段:

    Sub getPageFieldInFooter()
    ' get current section number
    Dim sectionNum As Integer
    sectionNum = Selection.Range.Information(wdActiveEndSectionNumber)
    'select first page footer, loop through its fields and find Page field
    ActiveDocument.Sections(sectionNum).Footers(wdHeaderFooterPrimary).Range.Select
    Dim f As Field
    For Each f In Selection.Fields
        If f.Type = wdFieldPage Then
            ' do something with the page data
            MsgBox f.Data
        End If
    Next f
    End Sub
    

    这种方法的输出是'1-1' 它无法工作的原因是因为它只能检索第一页(或使用wdHeaderFooterEvenPages检索第二页)。

  2. 罗马数字格式或该列表中的任何其他格式也是如此 对于以下页码设置,我希望在特定页脚中获得“值” 上面的代码将返回第一页或第二页的值,就是这样。 Roman Number format

  3. 有没有办法访问文档中的任何页脚并执行我的代码示例? 如果没有,我如何获得我选择的任何页脚的页码“值”? 希望这更清楚。

3 个答案:

答案 0 :(得分:2)

以下对我有用,虽然我不确定它有多可靠。显然,如果我查询文档中当前选择的页脚(或页眉),它将返回该页面页脚(或页眉)的信息。

一旦开始使用多个部分和不同的第一页,事情就变得非常复杂。我已经在下面的代码中做了一些测试,但我不会发誓它是“生产代码”。但是,它应该给你一个起点。

Sub GetFormattedPageNumberFromSelection()
    Dim sel As word.Selection
    Dim sec As word.Section
    Dim r As word.Range, rOriginal As word.Range
    Dim fld As word.Field
    Dim secCurrIndex As Long
    Dim sNoPageNumber As String

    Set sel = Selection
    If Not sel.InRange(sel.Document.content) Then Exit Sub

    Set sec = sel.Sections(1)
    If Not sec.Footers(wdHeaderFooterFirstPage).exists Then
        Set r = sec.Footers(wdHeaderFooterPrimary).Range
    Else
        Set r = sel.Range
        Set rOriginal = r.Duplicate
        secCurrIndex = sec.index
        If secCurrIndex <> 1 Then
            sel.GoToPrevious wdGoToPage
            If sel.Sections(1).index = secCurrIndex Then
                Set r = sec.Footers(wdHeaderFooterPrimary).Range
            Else
                Set r = sec.Footers(wdHeaderFooterFirstPage).Range
            End If
            rOriginal.Select 'return to original selection
        ElseIf r.Information(wdActiveEndPageNumber) = 1 Then
            Set r = sec.Footers(wdHeaderFooterFirstPage).Range
        Else
            Set r = sec.Footers(wdHeaderFooterPrimary).Range
        End If
    End If
    For Each fld In r.Fields
        sNoPageNumber = "No page number"
        If fld.Type = wdFieldPage Then
            Debug.Print fld.result
            sNoPageNumber = ""
            Exit For
        End If
    Next
    If Len(sNoPageNumber) > 0 Then Debug.Print sNoPageNumber
End Sub

答案 1 :(得分:2)

......有时我们看不到最简单的方法。

在当前选择中插入页面字段,读取结果,然后再次将其删除:

Sub GetFormattedPageNumberFromSelection2()
    Dim rng As word.Range
    Dim fld As word.Field

    Set rng = Selection.Range
    Set fld = rng.Fields.Add(rng, wdFieldPage)
    Debug.Print fld.result
    fld.Delete
End Sub

答案 2 :(得分:1)

您没有告诉我们的是您如何'选择'您想要参考的页面。假设它基于选择/显示的任何页面,您可以使用类似以下内容的页面标题

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, Fld As Field
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
For Each Fld In Selection.HeaderFooter.Range.Fields
  If Fld.Type = wdFieldPage Then
    MsgBox Fld.Result
    Exit For
  End If
Next
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
Application.ScreenUpdating = True
End Sub

不幸的是,wdSeekCurrentPageFooter会返回下一页的页脚!,因此您无法将其用于当前页脚。但是,以下内容应适用于PAGE#字段所在的位置:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long, Fld As Field, bExit As Boolean: bExit = False
With ActiveWindow.ActivePane.Pages(Selection.Information(wdActiveEndAdjustedPageNumber))
  For i = 1 To .Rectangles.Count
    With .Rectangles(i).Range
      For Each Fld In .Fields
        If Fld.Type = wdFieldPage Then
          MsgBox Fld.Result
          bExit = True: Exit For
        End If
      Next
    End With
    If bExit = True Then Exit For
  Next
End With
Application.ScreenUpdating = True
End Sub