Solved

Word VBA: How to get an ABSOLUTE line number?

Posted on 2000-02-23
9
2,668 Views
Last Modified: 2008-02-26
I'd like to get the ABSOLUTE line number (which means, count from the beginning of document, not the beginning of that page) of the first character of a selection (better yet, of any range). Any way to do this?

Some related codes I know of are :

[A] aRange.Information(wdFirstCharacterLineNumber)

and

[B] ActiveDocument.BuiltInDocumentProperties(wdPropertyLines)

But [A] gives a RELATIVE (count from the page) and [B] gives the total line count of the document.
0
Comment
Question by:shapeless
9 Comments
 
LVL 5

Expert Comment

by:vboukhar
ID: 2554594
I don't find built-in features in Word VBA too. I can propouse you only very restricted way - if your document has the same linespacing and font across entire document, you can calculate number of lines per page and get your line number from page number and relative line number (from page).
Something like that:
Sub test()
iPgNum = Selection.Information(wdActiveEndPageNumber)
iLnNum = Selection.Information(wdFirstCharacterLineNumber)
LnPerPage = Int(ActiveDocument.PageSetup.PageHeight / Selection.ParagraphFormat.LineSpacing) - 2
MsgBox LnPerPage * (iPgNum - 1) + iLnNum
End Sub

It returns correct number for file with the same font (Courier 9) and linespasing across the document.

To check it, you can turn on linenumbering -
ActiveDocument.PageSetup.LineNumbering.Active = True

Hope it helps.
0
 
LVL 13

Expert Comment

by:cri
ID: 2555124
The manual way would be
Shift+Ctrl+Home to expand selection to the beginning of the text, then use Tools¦Word Count to _display_ the number of lines.

Do not know if a recording of this is of use, i.e. whether you need line number as _value_.

In this case, how about this (crude) solution:
- SaveAs to a temp file,
- Shift+Ctrl+End, Delete
- File/Properties
- Go back to document  
0
 
LVL 44

Expert Comment

by:bruintje
ID: 2555544
Hi shapeless,

maybe this will help

set myDoc = Documents("MijnDocument")
With myDoc.PageSetup.LineNumbering
    .Active = True
    .StartingNumber = 1
    .CountBy = 5
    .RestartMode = wdRestartContinuous
End With

it will number the activedocument with a linenumber every 5 lines, but you can change this value(Countby) yourself....

HTH:O)Bruintje
0
 

Author Comment

by:shapeless
ID: 2556724
Hi all,

Thanks for the comments.

To vboukhar --
Your suggestion works but is too limited. Something like inserted figure or subscript/superscript will fail it.

To cri --
I don't understand what you mean "then use Tools¦Word Count to _display_ the number of lines" in your first suggestion.

Your 2nd suggestion, as you mention as a crude one, maybe the only way to do it (so far). I will leave this question open for sometime. If no other solution is offerred, I will accept it as the answer.

To Bruintje --
Sorry that I didn't make it clear enough in my question. I don't want to display/see the linenumber on the screen. I want the linenumber be returned as a value.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:vboukhar
ID: 2556988
shapeless,
Look at new way (based on cri suggestion):
it get lines from document properties, extends selection to the end, copy selection to new doc, get it's number of lines from document properties, close it and substruct one ficuge from other. Screen is turned off

Sub GetLineNum()
Dim iFir As Integer, iSec As Integer
iFir = ActiveDocument.BuiltInDocumentProperties("Number of lines")
ScreenUpdating = False
Set tmp = Selection
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Selection.Copy
Application.Documents.Add
Selection.Paste
iSec = ActiveDocument.BuiltInDocumentProperties("Number of lines")
Application.DisplayAlerts = wdAlertsNone
ActiveDocument.Close False
Application.DisplayAlerts = wdAlertsAll
MsgBox iFir - iSec
Selection.Collapse Direction:=wdCollapseStart
ScreenUpdating = True
End Sub

Hope it helps
0
 
LVL 5

Accepted Solution

by:
vboukhar earned 50 total points
ID: 2557001
I re-read cri's first suggestion - it's cool! Look at VBA sample of this way:
Sub OtherWay()
 Selection.HomeKey Unit:=wdStory, Extend:=wdExtend
 MsgBox Selection.Range.ComputeStatistics(wdStatisticLines)
 Selection.Collapse Direction:=wdCollapseStart
End Sub

Cri, my best regards!
0
 

Author Comment

by:shapeless
ID: 2557281
vboukhar,

Your code:

Selection.Range.ComputeStatistics(wdStatisticLines)

is exactly what I need. Thx. :) :)
0
 
LVL 5

Expert Comment

by:vboukhar
ID: 2557335
Thanks, shapeless
It was interesting question!

Cri,
I'll post 25-points question for you - it was your design and my implementation.
0
 
LVL 22

Expert Comment

by:ture
ID: 2557608
Nice!
/Ture
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
IF OR formula Excel 8 42
Outlook 2016 hangs on the Launch Screen 3 59
Word 2016 - Comments on one page contracted 2 45
TSQL DateADD update Question 4 25
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now