Hi Ken,
Since there's no line object in VBA, this type of operation is
always a bit klutzy. Here are 2 ways, both imperfect.
The first macro requires that the cell have uniform linespacing
and SpaceBefore/SpaceAfter set to 0. From my testing it also
appears the linespacing rule needs to be set as 'Exactly.' In
addition, you can't use this on a row that's allowed to break
across pages; all hell breaks loose at the Bottom-minus-Top
calculation. Finally, the wdVerticalPositionRelativeToPage
and related parameters of the .Information property are not,
I'm told, 100% reliable, esp. if the selection or range isn't
visible on the screen, although I've never encountered that
problem myself.
Sub CountLinesInCellUsingStartAndEndPositions()
Dim r As Range, TopPos As Single, BtmPos As Single
Set r = Selection.Cells(1).Range
r.MoveEnd wdCharacter, -1
TopPos = r.Information(wdVerticalPositionRelativeToPage)
r.Collapse wdCollapseEnd
BtmPos = r.Information(wdVerticalPositionRelativeToPage)
MsgBox "Cell has " & _
Int((BtmPos - TopPos) / r.ParagraphFormat.LineSpacing + 0.5) + 1 & _
" lines."
End Sub
The second macro essentially just goes to the end of each line
in the cell, then "presses" the right arrow key, increments a
counter, then checks to see if the selection has jumped a cell
border (checks to see if it's "in the range" of the original
cell). If it has, the value of counter is the number of lines
in the cell. This method is probably more flexible than the
first since it doesn't care what the linespacing is; but since
it uses the Selection it's likely to be slow, especially if
running within a loop on a big table.
Sub CountLinesInCellByCheckingRangeOfEachLine()
Dim r As Range, counter As Long
Set r = Selection.Cells(1).Range
r.MoveEnd wdCharacter, -1
r.Select
Selection.Collapse wdCollapseStart
Do Until Not Selection.InRange(r)
counter = counter + 1
Selection.EndKey wdLine
Selection.MoveRight wdCharacter, 1
Loop
Selection.MoveLeft wdCell, 1
Selection.Collapse wdCollapseStart
MsgBox "Cell has " & counter & " lines."
End Sub
Hope this helps a little.