Results 1 to 7 of 7
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Finding the number of lines in a paragraph (VBA Word 2000)

    This ought to be easy, but I can't seem to find it.

    How can you find the number of lines in a paragraph?

    Thanks!!
    Troy

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Finding the number of lines in a paragraph (VBA Word 2000)

    Try my new best friend, the ComputeStatistics method (e.g., in the Immediate window):

    ?selection.Paragraphs(1).Range.ComputeStatistics(w dStatisticLines)

  3. #3
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Finding the number of lines in a paragraph (VBA Word 2000)

    <P ID="edit" class=small>(Edited by TroyWells on 13-Dec-02 15:11. )</P>I love it!! Works great, is simple, and fast!

    EDIT: I did find one flaw with this method:

    If the right side of the last character of a line is within 2 pt of the right margin, the line is counted as two lines, but it only does that once per paragraph.

    For example, if the right side of the last character of the first and second line of a two line paragraph both come within 2 pt of the right margin, it is seen as 3 lines.

    You can also get an extra line if the entire paragraph is selected and
    - the preceeding paragraph (including paragraph mark) is a hyperlink
    - the preceding character is a page break or section break

    Thanks!!
    Troy

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Finding the number of lines in a paragraph (VBA Word 2000)

    My friendship with ComputeStatistics is on the rocks. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    I can't understand those results. Is the method using a different typeface? I just don't see how it could wrap a line that doesn't wrap in any of the view modes (Normal or Print Layout or Print Preview) unless it changed the typeface. Why would it do that?

    Anyway, all the other possible ways to calculate this "manually" seem pretty messy, so if this is "close enough," I''ll give up here.

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Finding the number of lines in a paragraph (VBA Word 2000)

    Troy,

    Does this one suffer the same fate?:

    <pre>Public Sub GetNumOfLines2()
    Dim lngLinesCt As Long
    Application.ScreenUpdating = False
    Selection.Paragraphs(1).Range.Select
    With Dialogs(wdDialogToolsWordCount)
    lngLinesCt = .Lines
    .Execute
    End With
    MsgBox CStr(lngLinesCt)
    Selection.Collapse
    Application.ScreenUpdating = True
    End Sub
    </pre>

    Gary

  6. #6
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Finding the number of lines in a paragraph (VBA Word 2000)

    I still found it useful. I just had to account for its flaws. This is the code snippet I used:

    By the way, I'm looking for two line headings and assuming I won't find any that are 3 lines or longer. I could compensate for that if need be. I just didn't see the need yet.

    Also, the lines "GoTo NextHeading" skip to the end of this code snippet to find the next victim to test. They assume that the heading is in fact 1 line

    <pre> 'Here I get the inital count of lines. If it is one, I look for the next heading.
    If Selection.Paragraphs(1).Range.ComputeStatistics(wd StatisticLines) = 1 Then GoTo NextHeading
    'This next line I use to get around the problem with the first character of the find being a page/section break. _
    I'm using a function to check the Unicode for the character that I swiped from someone on this forum. _
    For some reason it sees both the page break and section break as = 12. If the line count is 3, then _
    there is still a two line heading, so I continue processing. Otherwise, I go the next heading.
    If cmd_DisplayAllCharacterCode(strText = Selection.Characters(1)) = 12 And _
    Selection.Paragraphs(1).Range.ComputeStatistics(wd StatisticLines) < 3 Then
    GoTo NextHeading
    End If
    'The next three lines check the preceding character to the selection. If this were a hyperlink, _
    the selection would almost always contain more than one character because the entire hyperlink _
    would be selected instead of just the first character of it. This, of course, assumes that the hyerlink _
    is more than just the paragraph marker in length.
    Selection.MoveLeft
    Selection.MoveLeft Extend:=wdExtend
    If Selection.Characters.Count > 1 Then GoTo NextHeading
    'The next three lines go to the end of the paragraph in question and check the position to see if it is _
    within 2 pts of the left margin, which in my case is 450 pt.
    Selection.MoveDown Unit:=wdParagraph, Count:=2
    Selection.MoveLeft
    If Selection.Information(wdHorizontalPositionRelative ToTextBoundary) > 447.9 Then GoTo NextHeading

    NextHeading:
    Selection.MoveDown Unit:=wdParagraph
    Selection.Find.Execute
    Wend</pre>


    That may seem like a lot to get around the shortcomings, but it works. I'm still glad you showed me ComputeStatistics. With the safeguards I've got in place, it does what I want it to do.

    Thanks!!
    Troy

  7. #7
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Finding the number of lines in a paragraph (VBA Word 2000)

    Gary,
    I tested this and found it DOES prevent the problem with coming within 2 pt of the right margin. Your procedure detected that as 1 line.

    However, your procedure still returns an extra line if the entire paragraph is selected and
    - the preceeding paragraph (including paragraph mark) is a hyperlink
    - the preceding character is a page break or section break

    Please see my last reply to jscher2000 with my workarounds.

    Thanks!!
    Troy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •