Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Word VBA: How to get an ABSOLUTE line number?

Posted on 2000-02-23
9
2,786 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

856 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