Link to home
Start Free TrialLog in
Avatar of PeterFrb
PeterFrbFlag for United States of America

asked on

Looking for elegant VB method of returning absolute line number in Word document

I have written VB programming code that captures a the absolute line number of a Word document's cursor.  I figured I would go ahead and write it clumsily, and then go back and try for elegance.  Using "Selection.Information(wdFirstCharacterLineNumber)" returns the line number within the context of a page, not the whole document.  In order to find the line number within the document, I've captured the line counts on each individual page and summed them to find the cursor location.  

I am able to use the "GoTo" feature to go to the absolute line number specified.  I have not yet found a means of finding the line number without taking the preliminary steps defined below:

Function ReturnLineCountsPerPage() As Integer()
    Dim iLinesPerPage() As Integer
    Dim iPages As Integer

    'Determine the total number of pages in the document: assign to iPages
    ActiveDocument.Repaginate
    iPages = ActiveDocument.BuiltInDocumentProperties(wdPropertyPages)
    
    'Create array of integers iLinesPerPage: As the array starts at index zero and the document starts on page 1, the array index is offset one down from the
    'actual page number.
    ReDim iLinesPerPage(0 To iPages - 1)
    For iCount = 1 To iPages
        Selection.GoTo What:=wdGoToPage, Which:=wdGoToFirst, Count:=iCount + 1
        Selection.MoveUp Unit:=wdLine, Count:=1
        iLinesPerPage(iCount - 1) = Selection.Information(wdFirstCharacterLineNumber)
    Next iCount
    ReturnLineCountsPerPage = iLinesPerPage
End Function

Open in new window

Capturing the lines on each page simplifies the math involved in finding the cursor's document-wide line number, but I would be surprised if VB did not have a built-in mechanism that would make this kluge unnecessary.  Does anyone know what that might be?

Thanks, ~Peter Ferber
Sub IdentifyLineNumber()
    Dim iLinesPerPage() as Integer

    'Pre-capture the lines per page, so as to allow the function to return the line count
    'without moving the cursor from its current location. 
    iLinesPerPage = ReturnLineCountsPerPage()

    'With the lines-per-page set, do some stuff, and then capture the line number in a variable...
    iStrategicLine  = ReturnLineNumber(iLinesPerPage)

    '...do some more stuff, and then go right back to the strategic line in question.
   Selection.GoTo What:=wdGoToLine, Which:=wdGoToFirst, Count:=iStrategicLine, Name:=""
End Sub

Function ReturnLineNumber(iLineCounts() As Integer) As Long
    Dim iCount As Integer
    Dim iPage As Integer
    Dim iLine As Integer
    
    iLine = Selection.Information(wdFirstCharacterLineNumber)
    iPage = Selection.Information(wdActiveEndPageNumber)
    
    ReturnLineNumber = 0
    'Add the line counts of all the previous pages together with the line count on the current page to derive the cursor's current line count.
    'iLineCounts() is pre-calculated, so as to return a line count without having to move the cursor from its current location.
    For iCount = 1 To iPage - 1
        ReturnLineNumber = ReturnLineNumber + iLineCounts(iCount - 1)
    Next iCount
    ReturnLineNumber = ReturnLineNumber + iLine
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of SStory
SStory
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PeterFrb

ASKER

Sorry, I got a notice that I"d left this question unattended.  The link by SStory has quite a bit of material there, but it appears to address my issue.  I will take a closer look and experiment with the code listed there; and hopefully, I'll put this question to bed in short order.  Sorry for the delay!
Sorry for the late approval.  Yes, this worked.