如何使用Word VBA在上一页上读取标题的文本?

发布于 2025-02-09 18:16:53 字数 539 浏览 2 评论 0原文

首先,我制作一个带有2页的Word Doc。

然后,我添加了一个标题,例如

Current {page}, total {numpages}

{page}{numpages}是字段代码。因此,Currnet 1,总计2在第一页上显示,而Currnet 2,总计2在第二页上显示。

我的问题是,在我的情况下,我们如何在上一个页面上读取标题的文本,这应该是Currnet 2,总计2

我只能写只

Sub a()

    MsgBox ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text
    
End Sub

写第一页上的文本,即Currnet 1,总计2

有帮助吗?谢谢。

Firstly, I make a WORD doc with 2 pages.

Then, I add a header like

Current {page}, total {numpages}

where {page} and {numpages} are field codes. So Currnet 1, total 2 is shown on the first page, while Currnet 2, total 2 is shown on the 2nd page.

My question is, how can we read the header's text on last page, in my case, which should be Currnet 2, total 2.

I can but only write

Sub a()

    MsgBox ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text
    
End Sub

which can only read the text on first page, i.e. Currnet 1, total 2.

Any help? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猫卆 2025-02-16 18:16:53

是的,您可以:

Sub GetLastPageHeader()
Application.ScreenUpdating = False
Dim Rng As Range
Set Rng = Selection.Range
Selection.GoTo What:=wdGoToPage, Count:=ActiveDocument.ComputeStatistics(wdStatisticPages)
With ActiveWindow
  If .View.SplitSpecial <> wdPaneNone Then .Panes(2).Close
  .ActivePane.View.Type = wdPrintView
  .ActivePane.View.SeekView = wdSeekCurrentPageHeader
  .ActivePane.Selection.MoveEndUntil vbCr, wdForward
  MsgBox Selection.Text
  .ActivePane.View.SeekView = wdSeekMainDocument
End With
Rng.Select
Application.ScreenUpdating = True
End Sub

要获取当前部分的最后一页标题,请替换

Selection.GoTo What:=wdGoToPage, Count:=ActiveDocument.ComputeStatistics(wdStatisticPages)

Selection.GoTo What:=wdGoToPage, Count:=ActiveDocument.Range(0, Selection.Sections(1).Range.End).ComputeStatistics(wdStatisticPages)

Yes, you can:

Sub GetLastPageHeader()
Application.ScreenUpdating = False
Dim Rng As Range
Set Rng = Selection.Range
Selection.GoTo What:=wdGoToPage, Count:=ActiveDocument.ComputeStatistics(wdStatisticPages)
With ActiveWindow
  If .View.SplitSpecial <> wdPaneNone Then .Panes(2).Close
  .ActivePane.View.Type = wdPrintView
  .ActivePane.View.SeekView = wdSeekCurrentPageHeader
  .ActivePane.Selection.MoveEndUntil vbCr, wdForward
  MsgBox Selection.Text
  .ActivePane.View.SeekView = wdSeekMainDocument
End With
Rng.Select
Application.ScreenUpdating = True
End Sub

To get the last page header for the current Section, replace:

Selection.GoTo What:=wdGoToPage, Count:=ActiveDocument.ComputeStatistics(wdStatisticPages)

with:

Selection.GoTo What:=wdGoToPage, Count:=ActiveDocument.Range(0, Selection.Sections(1).Range.End).ComputeStatistics(wdStatisticPages)
苍暮颜 2025-02-16 18:16:53

获取有关每个二手标头的报告&amp;页脚,您可能会使用以下内容:

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, Sctn As Section, HdFt As HeaderFooter, Fld As Field, StrHdFt As String
With ActiveDocument
  For Each Sctn In .Sections
    For Each HdFt In Sctn.Headers
      With HdFt
        If .Exists Then
          Select Case .Index
            Case wdHeaderFooterEvenPages: StrHdFt = "Section: " & Sctn.Index & ", Even Pages Header" & vbCr & vbCr & "Fields:"
            Case wdHeaderFooterFirstPage: StrHdFt = "Section: " & Sctn.Index & ", First Page Header" & vbCr & vbCr & "Fields:"
            Case wdHeaderFooterPrimary: StrHdFt = "Section: " & Sctn.Index & ", Primary Header" & vbCr & vbCr & "Fields:"
          End Select
          For Each Fld In .Range.Fields
            With Fld
              Select Case .Type
                Case wdFieldNumPages, wdFieldPage, wdFieldSection, wdFieldSectionPages
                  StrHdFt = StrHdFt & vbCr & "{" & .Code.Text & "}"
                Case Else
              End Select
            End With
          Next
          MsgBox "Text: " & .Range.Text & vbCr & StrHdFt
        End If
      End With
    Next
    For Each HdFt In Sctn.Footers
      With HdFt
        If .Exists Then
          Select Case .Index
            Case wdHeaderFooterEvenPages: StrHdFt = "Section: " & Sctn.Index & ", Even Pages Footer" & vbCr & vbCr & "Fields:"
            Case wdHeaderFooterFirstPage: StrHdFt = "Section: " & Sctn.Index & ", First Page Footer" & vbCr & vbCr & "Fields:"
            Case wdHeaderFooterPrimary: StrHdFt = "Section: " & Sctn.Index & ", Primary Footer" & vbCr & vbCr & "Fields:"
          End Select
          For Each Fld In .Range.Fields
            With Fld
              Select Case .Type
                Case wdFieldNumPages, wdFieldPage, wdFieldSection, wdFieldSectionPages
                  StrHdFt = StrHdFt & vbCr & "{" & .Code.Text & "}"
                Case Else
              End Select
            End With
          Next
          MsgBox "Text: " & .Range.Text & vbCr & StrHdFt
        End If
      End With
    Next
  Next
End With
End Sub

不用担心文本字符串仅适用于每个部分中每个标头/页脚中的第一页。重要的是存在所需的字段。

To get a report on every used Header & footer, you might use something like:

Sub Demo()
Application.ScreenUpdating = False
Dim Rng As Range, Sctn As Section, HdFt As HeaderFooter, Fld As Field, StrHdFt As String
With ActiveDocument
  For Each Sctn In .Sections
    For Each HdFt In Sctn.Headers
      With HdFt
        If .Exists Then
          Select Case .Index
            Case wdHeaderFooterEvenPages: StrHdFt = "Section: " & Sctn.Index & ", Even Pages Header" & vbCr & vbCr & "Fields:"
            Case wdHeaderFooterFirstPage: StrHdFt = "Section: " & Sctn.Index & ", First Page Header" & vbCr & vbCr & "Fields:"
            Case wdHeaderFooterPrimary: StrHdFt = "Section: " & Sctn.Index & ", Primary Header" & vbCr & vbCr & "Fields:"
          End Select
          For Each Fld In .Range.Fields
            With Fld
              Select Case .Type
                Case wdFieldNumPages, wdFieldPage, wdFieldSection, wdFieldSectionPages
                  StrHdFt = StrHdFt & vbCr & "{" & .Code.Text & "}"
                Case Else
              End Select
            End With
          Next
          MsgBox "Text: " & .Range.Text & vbCr & StrHdFt
        End If
      End With
    Next
    For Each HdFt In Sctn.Footers
      With HdFt
        If .Exists Then
          Select Case .Index
            Case wdHeaderFooterEvenPages: StrHdFt = "Section: " & Sctn.Index & ", Even Pages Footer" & vbCr & vbCr & "Fields:"
            Case wdHeaderFooterFirstPage: StrHdFt = "Section: " & Sctn.Index & ", First Page Footer" & vbCr & vbCr & "Fields:"
            Case wdHeaderFooterPrimary: StrHdFt = "Section: " & Sctn.Index & ", Primary Footer" & vbCr & vbCr & "Fields:"
          End Select
          For Each Fld In .Range.Fields
            With Fld
              Select Case .Type
                Case wdFieldNumPages, wdFieldPage, wdFieldSection, wdFieldSectionPages
                  StrHdFt = StrHdFt & vbCr & "{" & .Code.Text & "}"
                Case Else
              End Select
            End With
          Next
          MsgBox "Text: " & .Range.Text & vbCr & StrHdFt
        End If
      End With
    Next
  Next
End With
End Sub

Don't worry that the text string is only for the first page in each Header/Footer in each Section. What matters is that the required fields are present.

沫雨熙 2025-02-16 18:16:53

你不能。

但是,如果您想获取文档的最后一页的数量,则可以使用

ActiveDocument.Characters.Last.Information(wdActiveEndPageNumber)

并获取页面数量

ActiveDocument.Characters.Last.Information(wdNumberOfPagesInDocument)

You can't.

But, if you want to get the number of the last page of the document you can use

ActiveDocument.Characters.Last.Information(wdActiveEndPageNumber)

And to get the number of pages

ActiveDocument.Characters.Last.Information(wdNumberOfPagesInDocument)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文