none
How to I return the page number of a page that contains a selection in Word 2010? RRS feed

  • Question

  • How do I return the page number from the Footer of the page that I have made a selection?  Here is my problem:

    When I use: 

    Dim PageNum As String

    PageNum = Selection.Information(wdActiveEndAdjustedPageNumber)

    Selection.TypeText ", Page " + PageNum

    Selection.TypeParagraph 

    It returns the page number perfectly, but if my Footer includes a Chapter number and the Chapter starts with a style (in this case "Heading 7") the wdActiveEndAdjustedPageNumber only returns the the page number and not the A-page number.  I need the code to return A-1, A-2... or B-1, B-2....C-1, C-2.. etc.  

    I guess my question is...How do I return the page number exactly as it appears in the footer?


    • Edited by Jimmy0306 Monday, July 21, 2014 9:24 PM
    Monday, July 21, 2014 9:24 PM

Answers

  • It's a bit difficult without knowing what else is in the document, nor where exactly in relation to the text of the document the text is to be inserted, but based on your question, the following should work

    Dim oRng As Range
    Dim PageNum As String
        Set oRng = Selection.Range
        PageNum = oRng.Information(wdActiveEndAdjustedPageNumber)

        With oRng
            .Text = vbCr
            .Collapse wdCollapseStart
            .Text = ", Page "
            .Collapse wdCollapseEnd
            .InsertCrossReference ReferenceType:="Heading", ReferenceKind:= _
                                  wdNumberRelativeContext, ReferenceItem:="3"
            .MoveEndUntil  Chr(13)
            .Fields.Unlink
            .Collapse wdCollapseEnd
            .Text = "-" & PageNum
            .Paragraphs(1).Range.Characters.Last.Delete
            .Collapse wdCollapseEnd
            .Select
        End With


    Graham Mayor - Word MVP
    www.gmayor.com



    Wednesday, July 23, 2014 5:20 AM