number of lines a cell

J

Jonas

What method would I use to calculate the number of lines in a cell within a
table programmatically?
 
H

Helmut Weber

Hi Jonas,
one method:

Sub LinesInCell()
Dim z1 As Integer
Dim z2 As Integer
With Selection
.Cells(1).Select
z1 = .Information(wdFirstCharacterLineNumber)
.EndKey
z2 = .Information(wdFirstCharacterLineNumber)
End With
MsgBox z2 - z1 + 1
End Sub

But not reliable anymore, as in Word 2002
unlike in Word 97 all too often
..Information(wdFirstCharacterLineNumber) returns -1,
and as this does not provide for cells reaching over
page breaks.

Another method:

Sub LinesInCell_1()
Dim r As Long
Dim l As Long
With Selection
.Cells(1).Select
.Collapse
l = 0
r = .Cells(1).RowIndex
On Error Resume Next
Do Until r <> .Cells(1).RowIndex
If Err.Number = 5941 Then
Err.Clear
Exit Do
End If
l = l + 1
.MoveDown
Loop
.MoveUp ' or better restore original selection
End With
MsgBox l
End Sub

Counts lines even if the cell spans over several pages.
You might add some lines to get the selection back
to where it was before doing the counting.
And better rewrite it to a function.

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top