Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Word VBA: How to get an ABSOLUTE line number?

Posted on 2000-02-23
9
Medium Priority
?
3,207 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

610 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