eshurak
asked on
Using VBA how to get real page number from MS Word
Hello,
Using VBA in Word I need to return the "true" page number of a Range. The code I'm using below is close, but the if the page number is "IV" (i.e. formatted in roman numerals ) it will return a "4" Is there a way to return the adjusted page number including the format. The same document may or may not contain multiple page number formats not just roman numerals so it is not just a matter of changing number format.
Using VBA in Word I need to return the "true" page number of a Range. The code I'm using below is close, but the if the page number is "IV" (i.e. formatted in roman numerals ) it will return a "4" Is there a way to return the adjusted page number including the format. The same document may or may not contain multiple page number formats not just roman numerals so it is not just a matter of changing number format.
Function GetPageNumber(ByVal DaRange As Range) As String
GetPageNumber = DaRange.Information(wdActiveEndAdjustedPageNumber)
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for both of your responses.
I'm going to use a modified version of Graham's solution.
It will get any page number format used no matter what Number Style is being used.
I'm also using fld.Result instead of change the rng object to get the page number.
I'm going to use a modified version of Graham's solution.
It will get any page number format used no matter what Number Style is being used.
I'm also using fld.Result instead of change the rng object to get the page number.
Function GetPageNumberDisplay(rng As Range) As String
Dim fld As Field
if rng.Sections(1).footers(1).PageNumbers.NumberStyle <> wdPageNumberStyleArabic Then
rng.Collapse wdCollapseEnd
Set fld = ActiveDocument.Fields.Add(rng, wdFieldPage)
GetPageNumberDisplay = fld.Result
fld.Delete
Set fld = Nothing
else
GetPageNumber = rng.Information(wdActiveEndAdjustedPageNumber)
End if
End Function
Open in new window