none
Information call returns wrong value for line and page numbers RRS feed

  • Question

  • Question
    You cannot vote on your own post
    0

    #1 I have been trying to use the wd constant but it always returns the same value no matter what line I'm on, ie, it doesn't match the line number shown in the status bar.

    Set user_rng = Selection.Range

    ... Set lineno_range = user_rng lineno_range.Collapse Direction:=wdCollapseEnd lineno = lineno_range.Information(wdFirstCharacterLineNumber)


    according to "help": wdFirstCharacterLineNumber ... If the selection or range is collapsed, the character number immediately to the right of the range or selection is returned (this is the same as the character line number displayed in the status bar after "Ln").

    #2 if the selection is in an endnote, the page number returned is not the page the end note is on

    pgno = user_range.Information(wdActiveEndAdjustedPageNumber)
    the return page number seems to be where the end note number is defined.


    David K. Ream

    Wednesday, March 1, 2017 3:17 PM

Answers

  • thank you. as to #1, it works but I had to move the .Information closer to the range.

    David K. Ream

    • Marked as answer by Dave Ream Thursday, March 2, 2017 12:03 PM
    Thursday, March 2, 2017 12:02 PM
  • thank you. as to #1, it works but I had to move the .Information closer to the range.


    David K. Ream

    • Marked as answer by Dave Ream Thursday, March 2, 2017 12:03 PM
    Thursday, March 2, 2017 12:03 PM

All replies

  • Hello,

    >>it always returns the same value no matter what line I'm on, ie, it doesn't match the line number shown in the status bar.

    According to WdInformation Enumeration:

    Returns the character position of the first character in the specified selection or range. If the selection or range is collapsed, the character number immediately to the right of the range or selection is returned (this is the same as the character line number displayed in the status bar after "Ln").

    So I test with the same code, it could return the same as the number displayed in the status bar after "Ln".

    >>the return page number seems to be where the end note number is defined.

    I get the same result. To work around, I suggest you return the last page or section number if current selection is an EndNote.

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 2, 2017 9:03 AM
    Moderator
  • Provided the selection is in the document body, the following works fine for me:

    Sub LineTest()
    Dim user_rng As Range, lineno_range As Range, lineno As Long
    Set user_rng = Selection.Range
    Set lineno_range = user_rng
    lineno_range.Collapse Direction:=wdCollapseEnd
    lineno = lineno_range.Information(wdFirstCharacterLineNumber)
    MsgBox lineno
    End Sub

    The returned value corresponds with the line# reported on the status bar.

    Word doesn't report line numbers for footnotes or endnotes and getting the page # for a selected endnote's text requires using the rectangles collection. For example:

    Sub EndNotePageTest()
    Dim i As Long, j As Long, Rct As Rectangle, rng As Range, bFnd As Boolean
    bFnd = False
    With ActiveDocument
      j = Selection.Information(wdActiveEndAdjustedPageNumber)
      For i = Selection.Information(wdActiveEndPageNumber) To .ComputeStatistics(wdStatisticPages)
        With .ActiveWindow.Panes(1).Pages(i)
          For Each Rct In .Rectangles
            If Selection.InRange(Rct.Range) Then
              MsgBox "The selection is on page: " & j: bFnd = True: Exit For
            End If
          Next
        End With
        If bFnd = True Then Exit For
        j = j + 1
      Next
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by macropodMVP Thursday, March 2, 2017 10:14 PM
    Thursday, March 2, 2017 11:07 AM
  • thank you. as to #1, it works but I had to move the .Information closer to the range.

    David K. Ream

    • Marked as answer by Dave Ream Thursday, March 2, 2017 12:03 PM
    Thursday, March 2, 2017 12:02 PM
  • thank you. as to #1, it works but I had to move the .Information closer to the range.


    David K. Ream

    • Marked as answer by Dave Ream Thursday, March 2, 2017 12:03 PM
    Thursday, March 2, 2017 12:03 PM