Solved

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

Posted on 2001-08-30
10
680 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
Comment Utility
Duplicated question, please delete it.
0
 
LVL 17

Expert Comment

by:calacuccia
Comment Utility
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
Comment Utility
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
Comment Utility
Hi calacuccia,

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

Author Comment

by:rernst
Comment Utility
Hi calacuccia,

 I tried it. However "nolines" is always either 0 or negative.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 9

Expert Comment

by:Valliappan AN
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you calacuccia, that's it finally.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

744 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

13 Experts available now in Live!

Get 1:1 Help Now