Results 1 to 8 of 8
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    any code to count lines in a selection?

    Hiya

    Does anyone have any code that obtains & counts the total lines (lines needed not paragraphs ) in a selection?
    as the code i'm using is returning values that are incorrect & really dodgy.

    many thanks Diana

  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: any code to count lines in a selection?

    Have you tried the ComputeStatistics method? It might require a range (e.g., Selection.Range.ComputeStatistics(type) or something like that).

  3. #3
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: any code to count lines in a selection?

    Hiya

    Yes I've used ComputeStatistics in the below code & i'm getting some seriously dodgy values returned.eg
    returns 1 when its supposed to return 5
    returns 48 when 48 doesnt exist.
    the below code is really unrealiable & I cant figure out why.
    So I wanted to see if there was some different code i could user.


    Public Function ObtainLineCount() As Long
    On Error GoTo ErrHandler
    ObtainLineCount = 0
    'select the text in the table cell area
    Selection.SelectRow
    Selection.Cells(1).Range.Select
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    ObtainLineCount = Selection.Range.ComputeStatistics(wdStatisticLines )
    Selection.Collapse wdCollapseStart
    Exit Function
    ErrHandler:
    MsgBox Err.Description
    End Function

    Diana

  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: any code to count lines in a selection?

    It must be your document. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

    Anyway, to preserve the user's selection, which has nothing to do with your question, you could switch to this "all range" solution:
    <pre>Public Function ObtainLineCount() As Long
    On Error GoTo ErrHandler
    ObtainLineCount = 0
    With Selection.Cells(1).Range
    ObtainLineCount = ActiveDocument.Range(.Start, .End - 1). _
    ComputeStatistics(wdStatisticLines)
    End With
    Exit Function
    ErrHandler:
    MsgBox Err.Description
    End Function</pre>

    I only tested on Word 2000 with the simplest of table rows. Can you see anything special in the cells that give weird results?

  5. #5
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: any code to count lines in a selection?

    thanks Jefferson

    I test that code & see if it works. I'll let you know if I have any problems.

    Diana

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

    Re: any code to count lines in a selection?

    Diana,

    There have been some previous threads on this, in which it may have come up that ComputeStatistics has got issues; of course can't find 'em at the moment...

    Have you tried Tools > Word Count? - just based on a quick test, I can't get this code to give a wrong result:

    <pre>Sub GetNumberOfLinesViaWordCount()

    Dim lngLines As Long

    With Dialogs(wdDialogToolsWordCount)
    .Execute
    lngLines = .Lines
    End With

    MsgBox lngLines

    End Sub
    </pre>

    Gary

  7. #7
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: any code to count lines in a selection?

    Hi Gary

    I've used both code ie ComputeStatitics & the code your've provided ie Dialogs(wdDialogToolsWordCount).lines & for both code occasionally the values returned were seriously dodgy.
    i dont believeits my orginial document template that the code is run over.
    So I've used the code below & it returns the correct value each time.

    cheers Diana

    Public Function ObtainLineCount() As Long
    Dim myrange As Range
    ObtainLineCount = 0
    Selection.SelectRow
    Selection.Cells(1).Range.Select
    Set myrange = Selection.Range
    myrange.Select
    Selection.Collapse wdCollapseStart
    While Selection.InRange(myrange) = True 'while the cursor is still in the current selection
    ObtainLineCount = ObtainLineCount + 1 'add to the line counter
    Selection.MoveDown Unit:=wdLine, Count:=1
    Wend
    myrange.Select
    Selection.Collapse wdCollapseStart
    End Function

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

    Re: any code to count lines in a selection?

    Diana,

    I should have read the code in the earlier posts more carefully 'cause I didn't factor in that you were doing this in a table cell - the reason for the dodgy results is that you were selecting the cell range, rather than just the text in the cell - you can demonstrate this problem using Word interactively as well.
    Looks like you've come up with a nice workaround! <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15>

    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
  •