Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Jul 2002
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Counting text lines in a table cell (Word 2002)

    Is there a way to count the number of text lines in a table cell? Using VBA, I am dropping the contents of an SQL database field into a table cell of a Word document. The text is generally long enough to wrap within the cell creating multiple lines. I want to determine using VBA code whether an even or odd number of text lines appear within the cell so that I can perform another action in an adjacent cell.

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

    Re: Counting text lines in a table cell (Word 2002)

    Try the following:

    <pre>Public Sub GetCountOfLinesInTableCell()

    Dim lngLinesCt As Long
    Selection.Cells(1).Range.Select
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend

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

    MsgBox CStr(lngLinesCt)

    Selection.Collapse wdCollapseStart

    End Sub
    </pre>

    Gary

  3. #3
    Star Lounger
    Join Date
    Mar 2001
    Location
    Cheltenham, Pennsylvania, USA
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting text lines in a table cell (Word 2002)

    An alternate tactic I've used in a similar problem (not related to a table cell) is to select each row and compare the value it returns for
    Selection.Information(wdFirstCharacterLineNumber)
    In your problem, subtract the line number of the first line from the line number of the last line. The difference (whether an odd or even number) gives your answer. Of course, if the last row's line number is lower than the first row's, you've crossed a page border. (Page borders were the focus of my original problem).

  4. #4
    New Lounger
    Join Date
    Jul 2002
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting text lines in a table cell (Word 2002)

    Thanks, Gary! Your suggestion works. I added one line after the Dim statement to go to the specific table in the document. Either of the following lines work to position the selection pointer.

    Selection.GoTo what:=wdGoToTable, which:=wdGoToFirst
    or
    Selection.GoTo what:=wdGoToTable, which:=wdGoToAbsolute, Count:=1

Posting Permissions

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