Solved

VBA for Word: How to determine the line count of a paragraph?

Posted on 2001-08-30
10
688 Views
Last Modified: 2008-02-26
How can I determine the number of lines in a paragraph? In the VBA help files I didn't find a property like e.g. "LineCount" nor any method to retrieve it. Is there a way at all to do this?
0
Comment
Question by:rernst
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 6439563
Duplicated question, please delete it.
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 6439676
ryancys, I think you got the wrong question, I don't see any duplicate (or remst already deleted the other one).

remst,

Try this:

Sub CountLinesParagraph(parNumber As Long)
Dim noLines As Long
With ActiveDocument.Paragraphs(parNumber)
noLines ActiveDocument.Range(Start:=.Range.Start, End:=.Range.Start).Information(wdFirstCharacterLineNumber) - ActiveDocument.Range(Start:=.Range.End - 1, End:=.Range.End - 1).Information(wdFirstCharacterLineNumber)
End With
End Sub
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 6439678
Correction

Sub CountLinesParagraph(parNumber As Long)
Dim noLines As Long
With ActiveDocument.Paragraphs(parNumber)
     noLines = ActiveDocument.Range(Start:=.Range.Start, End:=.Range.Start).Information(wdFirstCharacterLineNumber) _
    - ActiveDocument.Range(Start:=.Range.End - 1, End:=.Range.End - 1).Information(wdFirstCharacterLineNumber)
End With
End Sub
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 50

Expert Comment

by:Ryan Chong
ID: 6439690
Hi calacuccia,

FYI, glad that you only see 1 similar question here. : )
0
 

Author Comment

by:rernst
ID: 6439774
Hi calacuccia,

 I tried it. However "nolines" is always either 0 or negative.
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6439806
hope you should subtract it reverse order, if I understood properly.

hey, look at the grading history..

0
 
LVL 17

Accepted Solution

by:
calacuccia earned 100 total points
ID: 6439816
Oops, you're right Yalli_An.

Sub CountLinesParagraph(parNumber As Long)
Dim noLines As Long
With ActiveDocument.Paragraphs(parNumber)
    noLines = ActiveDocument.Range(Start:=.Range.End, End:=.Range.End).Information(wdFirstCharacterLineNumber) _
   - ActiveDocument.Range(Start:=.Range.Start - 1, End:=.Range.Start - 1).Information(wdFirstCharacterLineNumber)
End With
End Sub
0
 

Author Comment

by:rernst
ID: 6439841
Ok. This version now works:

With ActiveDocument.Paragraphs(parNumber)
    noLines = ActiveDocument.Range(Start:=.Range.End, End:=.Range.End).Information(wdFirstCharacterLineNumber) - ActiveDocument.Range(Start:=.Range.Start, End:=.Range.Start).Information(wdFirstCharacterLineNumber)
End With

It gives me a runtime error '4608' on the last paragraph, though. Any idea why?
0
 
LVL 17

Expert Comment

by:calacuccia
ID: 6439955
Sorry, I  should have tested it more thoroughly.

Try this version

Sub CountLinesParagraph(parNumber As Long)
Dim noLines As Long
With ActiveDocument.Paragraphs(parNumber)
    noLines = ActiveDocument.Range(Start:=.Range.End - 1, End:=.Range.End - 1).Information(wdFirstCharacterLineNumber) _
   - ActiveDocument.Range(Start:=.Range.Start, End:=.Range.Start).Information(wdFirstCharacterLineNumber) + 1
End With
End Sub
0
 

Author Comment

by:rernst
ID: 6440327
Thank you calacuccia, that's it finally.
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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

813 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

16 Experts available now in Live!

Get 1:1 Help Now