Solved

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

Posted on 2001-08-30
10
689 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 51

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Determine Range to Select 5 55
vb6 connector to SQL Server 2 41
add criteria to query in VB, Access 2003 2 29
Assign a value in Excel V-Basic 3 28
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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 process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

713 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