PeterFrb
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(wdF irstCharac terLineNum ber)" 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:
Thanks, ~Peter Ferber
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry for the late approval. Yes, this worked.
ASKER