Results 1 to 8 of 8
  1. #1
    Infody
    Guest

    Number of Lines in a Selection

    I am using Word 97. Can anyone tell me how to determine how many lines I have in a selection. I found out how to find out how many sentences were in a selection, but I need to determine the number of lines. The reason I want to know this is to determine if the selection will require more than 1 page if I had additional text to the selection.

    Kind regards,

    Bob Dempsey
    bdempsey@charter.net

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

    Re: Number of Lines in a Selection

    Bob,

    The following macro should do this. Note however, that if the selection goes onto a second page, that the line count will restart.

    <pre>Sub NumberOfLinesInSelection()
    'NOTE: the count restarts with each page!
    Dim lngStartLine As Long
    Dim lngEndLine As Long
    Dim lngTotLines As Long
    Dim rngTemp As Range
    With Selection
    Set rngTemp = .Range
    lngStartLine = .Information(wdFirstCharacterLineNumber)
    .Collapse wdCollapseEnd
    lngEndLine = .Information(wdFirstCharacterLineNumber)
    lngTotLines = lngEndLine - lngStartLine
    rngTemp.Select
    MsgBox "Selection contains " & lngTotLines & " lines. "
    End With
    End Sub</pre>

    Gary

  3. #3
    PeterF
    Guest

    Re: Number of Lines in a Selection

    Before posting my query on this topic I did a search and came upon this thread.

    Gary, like Bob, I also want to access a line count for a selection. The Word Tools - Word Count menu option displays the Word Count dialog that returns a number of document or selection statistics including number of words and number of lines.

    I can program to return the word count of a document or selection with Selection.Words.Count however there is no Selection.Lines.Count.

    I would like to know if there is any way I can access Word's line count, hopefully without the issue of the count restarting if the selection goes on to a second page, as that presents an unwelcome complication.

    Any advice from you or anyone else on this would be greatly appreciated.

    PeterF

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

    Re: Number of Lines in a Selection

    Hi Peter,

    I'm glad you posted this, because it is possible to return the line count via Word Count, and it's actually a better method than the one posted previously, since it will give you a line count across page divisions.

    The following should do it:

    <pre>Public Sub GetLineCountViaWordCount()
    'This method gets a line count via Word's
    'Tools>Word Count function - this permits a
    'line count across pages
    Dim lngLines As Long
    With Dialogs(wdDialogToolsWordCount)
    .Execute
    lngLines = .Lines
    MsgBox "Current selection contains " & lngLines & " lines. "
    End With
    End Sub
    </pre>

    Gary

  5. #5
    PeterF
    Guest

    Re: Number of Lines in a Selection

    Thank you Gary for this macro. It solves my problem perfectly.

    I have also noted the general method you use for executing a Word Dialog, assigning a variable to a value it returns and then using that variable. Most informative and greatly appreciated.

    Best regards

    Peter F

  6. #6
    PeterF
    Guest

    Re: Number of Lines in a Selection

    Using Gary's excellent solution, my macro goes to the second cell of a table, counts the number of lines in it and returns this as a value in a custom dialog. However testing has revealed that if the cell is empty, the macro counts the number of lines in the entire document and returns this. The value can take quite a while (> 1 second) to be calculated and is wildly misleading. How can I get the macro to identify an empty cell so I can make it take "evasive action"?

    As always, your suggestions are greatly appreciated.

    PeterF

  7. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Number of Lines in a Selection

    You may need to post your code to show us how you 'go to a cell' but the likely solution would involve something along these lines.
    If Len(Selection.Cells(1).Range.text) = 2 Then
    MsgBox "Empty cell"
    End If
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  8. #8
    PeterF
    Guest

    Re: Number of Lines in a Selection

    Thanks Andrew. That did the trick. My code for "going to a cell" is quite simple:

    'go to the next table in the document
    Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext, Count:=1, Name:=""
    Selection.Find.ClearFormatting

    'move the cursor to the second cell
    Selection.EndKey Unit:=wdRow

    Your assistance is greatly appreciated.

    Peter F

Posting Permissions

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