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?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
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”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.
Our community of experts have been thoroughly vetted for their expertise and industry experience.