Home > Mobile >  how to use word vba to read header's text on last page?
how to use word vba to read header's text on last page?

Time:06-22

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.

CodePudding user response:

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)

CodePudding user response:

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 Section. What matters is that the required fields are present.

CodePudding user response:

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)
  • Related