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.
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)