?
Solved

Word VBA: How to get an ABSOLUTE line number?

Posted on 2000-02-23
9
Medium Priority
?
3,086 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
[X]
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
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
Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

764 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