Solved

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

Posted on 2001-08-30
10
685 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 49

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

920 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