troubleshooting Question

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

Avatar of PeterFrb
PeterFrbFlag for United States of America asked on
Visual Basic.NET
3 Comments1 Solution406 ViewsLast Modified:
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
    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)

    ' 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


Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros