Get current line number

S

skrimpy

I know this code will return a current line number


With ActiveDocument
MsgBox .Range(.Range.Start, Selection.End
1).ComputeStatistics(wdStatisticLines)

but it doesnt seem to work to count the lines if your entire documen
is a table. This code was the only way I could get the current lin
number. It sets a bookmark, simulates the cursor position afte
whatever manipulation your doing to your document, and then copares th
original to another bookmark to get the current line number. Im no
sure this is the most efficient way but it works.

'set bookmark at start of document
Selection.HomeKey unit:=wdStory
ActiveDocument.Bookmarks.Add Name:="Start"
Address1 = ActiveDocument.Bookmarks("Start").Range.Start

'simulation of a current selection
Selection.MoveDown unit:=wdLine, Count:=5

'Loop
Dim lineNumber As Integer
Dim Check As Boolean
lineNumber = 1
Check = False
Do While Check = False
ActiveDocument.Bookmarks.Add Name:="Line"
Address2 = ActiveDocument.Bookmarks("Line").Range.Start
If Address1 = Address2 Then
Check = True
End If
ActiveDocument.Bookmarks("Line").Delete
Selection.MoveUp unit:=wdLine, Count:=1
lineNumber = lineNumber + 1
Loo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top