Results 1 to 12 of 12
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I couldn't find an example on the web so I rolled my own.
    No guarantees, and if I know The Lounge, within 15 minutes there will be three better methods ....
    Word 2000
    Code:
    Function lngLinesInParagraph(rng As Range) As Long
        Dim lng As Long
        lng = rng.Expand(Unit:=wdParagraph)
        Dim rng2 As Range
        Set rng2 = rng
        rng2.End = rng2.End + lng
        Dim lngFirst As Long
        lngFirst = rng2.Paragraphs(1).Range.Information(wdFirstCharacterLineNumber)
        Dim lngLast As Long
        lngLast = rng2.Paragraphs(2).Range.Information(wdFirstCharacterLineNumber)
        lngLinesInParagraph = lngLast - lngFirst
    'Sub TESTlngLinesInParagraph()
    '    MsgBox lngLinesInParagraph(Selection.Range)
    'End Sub
    End Function
    (later) fails on the last paragraph of a document

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by chrisgreaves View Post
    No guarantees, and if I know The Lounge, within 15 minutes there will be three better methods ....Word 2000
    I was right:
    Code:
    Function lngLinesInParagraph(rng As Range) As Long
        ' 2010/01/06  CG  5059: The macro "SingleLineAllCaps" is not testing the constraint "single line"
        Dim lngFirst As Long
        Dim lnglast As Long
        lngFirst = rng.Paragraphs(1).Range.Characters(1).Information(wdFirstCharacterLineNumber)
        lnglast = rng.Paragraphs(1).Range.Characters(rng.Paragraphs(1).Range.Characters.Count).Information(wdFirstCharacterLineNumber)
        lngLinesInParagraph = lnglast - lngFirst + 1
    'Sub TESTlngLinesInParagraph()
    '    MsgBox lngLinesInParagraph(Selection.Range)
    'End Sub
    End Function
    Ugly but shorter, and better working; just like me ....

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

    Code:
    Function lngLinesInParagraph(rng As Range) As Long
        Dim lngFirst As Long
        Dim lnglast As Long
        lngFirst = rng.Paragraphs(1).Range.Characters.First.Information(wdFirstCharacterLineNumber)
        lnglast = rng.Paragraphs(1).Range.Characters.Last.Information(wdFirstCharacterLineNumber)
        lngLinesInParagraph = lnglast - lngFirst + 1
    End Function
    Gary

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Gary Frieder View Post
    Slightly shorter...
    ... and MUCH more readable. Thanks Gary.
    I've not played around much with the .Information, excepting for wdWithinTable.
    I should spend a quiet weekend playing with it I suppose ....

  5. #5
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi all,

    You'd better watch out for paragraphs that span more than one page! If you can be confident the paragraph will never occupy more than a full page, you could temporarily set the 'keep together' property before doing the line count.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  6. #6
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    Quote Originally Posted by macropod View Post
    Hi all,

    You'd better watch out for paragraphs that span more than one page! If you can be confident the paragraph will never occupy more than a full page, you could temporarily set the 'keep together' property before doing the line count.
    Thanks Paul, good point!

    Here's a different approach, that appears to work for paragraphs that span more than one page - haven't tested it thoroughly though.

    Code:
    Function lngLinesInParagraph(rng As Range) As Long
       Application.ScreenUpdating = False
       'Bookmark existing selection:
       ActiveDocument.Bookmarks.Add "bmkTempSelect", rng
       'Select the active paragraph:
       rng.Paragraphs(1).Range.Select
       'Add one to result - for reasons that are unclear, this dialog
       'programatically returns a number that is one less than the
       'number it returns via the user interface!
       lngLinesInParagraph = Dialogs(wdDialogToolsWordCount).Lines + 1
       'Reselect the original selection and delete temp bookmark:
       ActiveDocument.Bookmarks("bmkTempSelect").Range.Select
       ActiveDocument.Bookmarks("bmkTempSelect").Delete
       Application.ScreenUpdating = True
    'Sub TESTlngLinesInParagraph()
    '    MsgBox lngLinesInParagraph(Selection.Range)
    'End Sub
    End Function
    Gary

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by macropod View Post
    ... watch out for paragraphs that span more than one page! ...
    Thanks macropod! (slinks back into depression in the ground muttering "When will I ever LEARN!")

  8. #8
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi Gary,

    With that approach in Word 2000, an empty paragraph counts as two lines, a paragraph with 5.4 lines of text counts as 6 lines and a paragraph with 5.6 lines of text counts as 7 lines ...
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  9. #9
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Hi Chris,

    Perhaps the following will do what you need:

    Code:
    Function lngLinesInParagraph(rng As Range) As Long
    Dim bState As Boolean, sngH As Single, lngFirst As Long, lnglast As Long
    Application.ScreenUpdating = False
    ' temporarily maximize the page height
    sngH = rng.Sections(1).PageSetup.PageHeight
    rng.Sections(1).PageSetup.PageHeight = InchesToPoints(22)
    With rng.Paragraphs(1)
    ' temporarily force the 'KeepTogether' attribute
      bState = .KeepTogether
      .KeepTogether = True
      With .Range.Characters
        lngFirst = .First.Information(wdFirstCharacterLineNumber)
        lnglast = .Last.Information(wdFirstCharacterLineNumber)
        'Add one to the difference to get an inclusive count
          lngLinesInParagraph = lnglast - lngFirst + 1
      End With
    ' restore the 'KeepTogether' status
      .KeepTogether = bState
    End With
    ' restore the page height
    rng.Sections(1).PageSetup.PageHeight = sngH
    Application.ScreenUpdating = True
    End Function
    
    Sub TESTlngLinesInParagraph()
        MsgBox lngLinesInParagraph(Selection.Range)
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  10. #10
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,055
    Thanks
    2
    Thanked 417 Times in 346 Posts
    hi Chris,

    Better still, why not use:
    Code:
    Sub TESTlngLinesInParagraph()
        MsgBox Selection.Paragraphs(1).Range.ComputeStatistics(wdStatisticLines)
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  11. #11
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by macropod View Post
    Better still, why not use:
    Selection.Paragraphs(1).Range.ComputeStatistics(wd StatisticLines)
    You know, Paul, I really hate it when you come up with gems like that.
    You and your buddies just do it to annoy me, don't you?

  12. #12
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    Quote Originally Posted by macropod View Post
    Better still, why not use:
    Code:
    Sub TESTlngLinesInParagraph()
        MsgBox Selection.Paragraphs(1).Range.ComputeStatistics(wdStatisticLines)
    End Sub
    That is a gem, thanks!
    Does make me wonder what is it that ComputeStatistics has access to internally, that enables it to do that, that we don't have access to...?

    Gary

Posting Permissions

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