Please help with text wrapping in table cells

K

Ken Wan

Hi,
In my VBA code, I need to find out how many lines of texts there are in a
table cell. Has anyone have a good suggestion how this can be achieved?
TIA,
Ken
 
J

Jezebel

You couldthe Information function to check the vertical position of each
character: each increment represents a new line. Or check the vertical
position of the first and last characters and divide by the line spacing.
 
M

Mark Tangard

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.
 
H

Helmut Weber

Hi Ken,
how about that:
where tx is a table, rx is a row, cx is a cell

Sub SelectCell(tx%, rx%, cx%)
ActiveDocument.Tables(tx).Cell(rx, cx).Select
End Sub

Public Function FncLinesInCell(tx%, rx%, cx%) As Integer
Dim z1 As Integer
Dim z2 As Integer
SelectCell tx, rx, cx
z1 = Selection.Information(wdFirstCharacterLineNumber)
Selection.EndKey
z2 = Selection.Information(wdFirstCharacterLineNumber)
FncLinesInCell = z2 - z1 + 1
End Function

Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, NT 4.0
 
M

Mark Baird

The only way I know of to reliably perform this task is to
use the selection object (Selection.MoveDown Unit:=wdLine)
and check the RowIndex every time the selection moves
counting the number of times that you have moved wihtin
that row.

Mark Baird
 

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