?
Solved

Using VBA how to get real page number from MS Word

Posted on 2010-09-03
3
Medium Priority
?
887 Views
Last Modified: 2013-11-25
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.

Function GetPageNumber(ByVal DaRange As Range) As String

    GetPageNumber = DaRange.Information(wdActiveEndAdjustedPageNumber)
    
End Function

Open in new window

0
Comment
Question by:eshurak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 8

Expert Comment

by:spinzr0
ID: 33600101
I'm not sure of a way to do this, but I did find this function which will convert to roman numerals on http://www.robvanderwoude.com/vbstech_data_roman.php
Function Decimal2Roman( ByVal intDecimal )
' This Function converts intDecimal to its Roman numeral value.
' Written by: Rob van der Woude, http://www.robvanderwoude.com
'
' intDecimal should be an integer in the range of 1..4999.
'
' For the Roman numeral "modern" notation is used, i.e. 1999
' will be written as MCMXCIX, not MIM.
'
' More information on Roman numerals can be found on WikiPedia:
' http://en.wikipedia.org/wiki/Roman_numerals

    ' Some housekeeping
    Dim strRoman
    strRoman = ""

    ' First, add an "M" for every multiple of 1000
    Do While intDecimal >= 1000
        intDecimal = intDecimal - 1000
        strRoman = strRoman & "M"
    Loop

    ' Next, add "CM" for 900, or "D" for 500, or "CD" for 400
    If intDecimal >= 900 Then
        intDecimal = intDecimal - 900
        strRoman = strRoman & "CM"
    ElseIf intDecimal >= 500 Then
        intDecimal = intDecimal - 500
        strRoman = strRoman & "D"
    ElseIf intDecimal >= 400 Then
        intDecimal = intDecimal - 400
        strRoman = strRoman & "CD"
    End If

    ' Add a "C" for every remaining multiple of 100
    Do While intDecimal >= 100
        intDecimal = intDecimal - 100
        strRoman = strRoman & "C"
    Loop

    ' Add "XC" for 90, or "L" for 50, or "XL" for 40
    If intDecimal >= 90 Then
        intDecimal = intDecimal - 90
        strRoman = strRoman & "XC"
    ElseIf intDecimal >= 50 Then
        intDecimal = intDecimal - 50
        strRoman = strRoman & "L"
    ElseIf intDecimal >= 40 Then
        intDecimal = intDecimal - 40
        strRoman = strRoman & "XL"
    End If

    ' Add an "X" for every remaining multiple of 10
    Do While intDecimal >= 10
        intDecimal = intDecimal - 10
        strRoman = strRoman & "X"
    Loop

    ' Add "IX" for 9, or "V" for 5, or "IV" for 4
    If intDecimal >= 9 Then
        intDecimal = intDecimal - 9
        strRoman = strRoman & "IX"
    ElseIf intDecimal >= 5 Then
        intDecimal = intDecimal - 5
        strRoman = strRoman & "V"
    ElseIf intDecimal >= 4 Then
        intDecimal = intDecimal - 4
        strRoman = strRoman & "IV"
    End If

    ' Finally, add an "I" for every remaining multiple of 1
    Do While intDecimal >= 1
        intDecimal = intDecimal - 1
        strRoman = strRoman & "I"
    Loop
    
    ' Return the result
    Decimal2Roman = strRoman
End Function

Open in new window

0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 33602298
I think the simplest way would be to insert a new Page field, capture the text, and then delete it.
Function GetPageNumberDisplay(rng As Range) As String
    Dim fld As Field
    
    rng.Collapse wdCollapseEnd
    Set fld = ActiveDocument.Fields.Add(rng, wdFieldPage)
    rng.Start = fld.Code.Start - 2
    rng.End = fld.Code.End + 3
    
    GetPageNumberDisplay = rng.Text
    fld.Delete
End Function

Open in new window

0
 
LVL 3

Author Comment

by:eshurak
ID: 33619589
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.
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

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question