Height or linecount of a cell?

B

Bear

This is a legal document application. In a row 1 of a table, cell 1 has a
variable amount of text, the number of lines may be determined by any
combination of paragraphs, linefeeds, or text wrapping.

In cell two, I want to use VBA to insert a series of right-parens and paras,
i.e. ")" & vbNewLine, until the height of cell two (or number of lines in
cell two) is equal to the original height (or number of lines) in cell one.

I've caught tantalizing snippets in various posts, but still have no way to
determine the acutual height of a row or cell, or the number of lines in a
cell or selection.

Can anyone help?

Bear
 
J

Jonathan West

Bear said:
This is a legal document application. In a row 1 of a table, cell 1 has a
variable amount of text, the number of lines may be determined by any
combination of paragraphs, linefeeds, or text wrapping.

In cell two, I want to use VBA to insert a series of right-parens and
paras,
i.e. ")" & vbNewLine, until the height of cell two (or number of lines in
cell two) is equal to the original height (or number of lines) in cell
one.

I've caught tantalizing snippets in various posts, but still have no way
to
determine the acutual height of a row or cell, or the number of lines in a
cell or selection.

Can anyone help?


Position the cursor at the start of the first cell. Use the
Selection.Information property to get the vertical position of the cursor.

Move the cursor to the start of the second row. Again use the
Selection.Information property to get the vertical position of the cursor.

Subtract one from the other to get the height of the first row. Set the
second row accordingly.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org
 
D

Doug Robbins - Word MVP

The following will only work if there is no space before are after the
definition of any of the paragraphs in the first cell

Dim cell1 As Range, i As Long, j As Long, lines As Long
Dim parens As String
parens = ""
With ActiveDocument.Tables(1)
Set cell1 = .Cell(1, 1).Range
cell1.Collapse wdCollapseStart
i = cell1.Information(wdFirstCharacterLineNumber)
Set cell1 = .Cell(1, 1).Range
cell1.End = cell1.End - 1
cell1.Collapse wdCollapseEnd
j = cell1.Information(wdFirstCharacterLineNumber)
lines = j - i
For i = 1 To lines
parens = parens & ")" & vbCr
Next i
parens = parens & ")"
.Cell(1, 2).Range.Text = parens
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
H

Helmut Weber

Hi Bear,

just another one:

Public Function LinesInCell(oCll As Cell) As Long
Dim y1 As Integer
Dim y2 As Integer
With oCll.Range.Characters
y1 = .First.Information(wdFirstCharacterLineNumber)
y2 = .Last.Previous.Information(wdFirstCharacterLineNumber)
If .Last.Previous = Chr(13) Or _
.Last.Previous = Chr(11) Then y2 = y2 + 1
End With
LinesInCell = y2 - y1 + 1
End Function

Sub Testd()
MsgBox LinesInCell(Selection.Cells(1))
End Sub

For cells spanning over pages, see:
http://tinyurl.com/33z9v2

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
B

Bear

Jonathan, Doug, and Helmut:

Thanks so much! Now I've got lots to experiment with.

Have you ever used Range.ComputeStatistics(wdStatisticLines)? Is it reliable?

Bear
 
H

Helmut Weber

Hi Bear,
Have you ever used Range.ComputeStatistics(wdStatisticLines)?
Is it reliable?

could get it to return anything else but 0 for a cell.

Haven't tried very hard, though.

Cheers

Helmut Weber MVP WordVBA
 
D

Doug Robbins - Word MVP

If just the text in the cell is selected,
Selection.Range.ComputeStatistics(wdStatisticLines) will return the number
of lines of text (with the limitation that I mentioned in respect of space
before after a paragraph). If the cell itself is selected, it returns 0 no
matter how many lines of text are contained in the cell.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

Bear

Doug and Helmut:

Yes, I found if I got the range of the cell, then moved the endpoint to the
left one character, I could get a nonzero return from ComputeStatistics. This
also seems to work well across page breaks.

Using ComputeStatistics in my application would create a vulnerability for
mismatches if the paragraph definitions (especially Spacing Before and After)
don't match.

Thanks again for your advice and cautions.

Bear

--
 
H

Helmut Weber

Hi Bear,

furthermore, the cell mark may occupy a line of it's own.
E.g. if there are three paragraphs in a cell,
all of which are empty,
then there are four lines in the cell.

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
B

Bear

Dorak:

Trouble? Yes and no. At some point it becomes a personal growth issue or a
personal challenge to find out how to solve a given problem. In the end we
all come out ahead.

These lists are great!

Bear
 

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