3

I've looked all over the place and tried various things. It's been assumed that it can't be done. So I'm going to try here and see if anybody else has had any luck.

Is there any way to get the height of a table row in Word when the row's HeightRule is set to wdRowHeightAuto?

Alternatively, if there's a way to get the cell's height instead, I'll accept that as a solution since you can calculate the row's height by finding the row's biggest cell.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Joe
  • 16,328
  • 12
  • 61
  • 75

3 Answers3

3

It's possible to find the row height with Range.Information(). The following snippet doesn't work for the last row in a table or the last row on a page

Dim Tbl as Table
Dim RowNo as Integer
Dim RowHeight as Double

' set Tbl and RowNo to the table and row number you want to measure

RowHeight=Tbl.Rows(RowNo+1).Range.Information(wdVerticalPositionRelativeToPage) _
    - Tbl.Rows(RowNo).Range.Information(wdVerticalPositionRelativeToPage)

This returns the height of the row in points by calculating the difference in position between the selected row and the following one.

I have a routine which works in all cases and returns the height in points of the second and subsequent lines in a cell, i.e. a single-line cell returns 0. (I use this in an application which reduces the font size in certain cells to fit the text on one line.)

Dim Doc As Document
Dim Tbl As Table

Dim Pos As Long
Dim RowNo As Integer
Dim ColNo As Integer
Dim CellHeight As Single

' set Doc, Tbl, RowNo and Colno to the document,table and row number you want to
' measure or provide a cell's range if you prefer

Pos = Tbl.Cell(RowNo, ColNo).Range.End - 1 ' last character in cell

CellHeight = Doc.Range(Pos, Pos).Information(wdVerticalPositionRelativeToTextBoundary)
grahamj42
  • 2,752
  • 3
  • 25
  • 34
1

I tried the above and found that changing the HeightRule changes the height of the row, which given I am trying to "freeze" the height on what appears in my table beforehand, makes nonsense of the above.

For rows which are empty or contain a single paragraph of unwrapped text in a consistent paragraph format adding up the font size, para before and after can work as follows:

Set r = c.Row
With r
   If .HeightRule <> wdRowHeightExactly Then
        .HeightRule = wdRowHeightExactly
         Set p = c.Range.ParagraphFormat
         .Height = c.BottomPadding + c.TopPadding + p.SpaceBefore + p.SpaceAfter +        p.LineSpacing
    End If
End With
eJulia
  • 11
  • 2
1

How about cheating?

Dim tbl As Word.Table
Dim r As Row
Dim c As Cell

Set tbl = ActiveDocument.Tables(1)

For Each r In tbl.Rows
    iHeight = r.HeightRule
    r.HeightRule = 1
    Debug.Print r.Height
    r.HeightRule = iHeight
Next
Fionnuala
  • 90,370
  • 7
  • 114
  • 152
  • This works in simple documents. There is other processing going on during the time I'm trying to get the height though. So I'm looking for a solution that doesn't involve changing the document in any way. – Joe Jan 17 '11 at 18:13
  • Can't find any better way to do it. – Joe Mar 01 '11 at 21:46