Solved

Word VBA: How to get an ABSOLUTE line number?

Posted on 2000-02-23
9
2,729 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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
 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

In case Office 2010 has not been deployed in your environment, this article may be quite useful. In our office, we wanted a way to deploy Microsoft Office Professional Plus 2010 through an automated batch file via logon script. This article is docum…
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

775 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