Vba 如何在Word中获取表格行的高度

Vba 如何在Word中获取表格行的高度,vba,ms-word,height,Vba,Ms Word,Height,我找遍了所有的地方,尝试了各种各样的东西。人们认为这是不可能的。所以我要在这里试试,看看有没有其他人运气好 当Word中表格行的HeightRule设置为wdRowHeightAuto时,是否有办法获取该行的高度 或者,如果有办法得到单元格的高度,我会接受它作为解决方案,因为您可以通过查找行的最大单元格来计算行的高度。作弊如何 Dim tbl As Word.Table Dim r As Row Dim c As Cell Set tbl = ActiveDocument.Tables(1)

我找遍了所有的地方,尝试了各种各样的东西。人们认为这是不可能的。所以我要在这里试试,看看有没有其他人运气好

当Word中表格行的
HeightRule
设置为
wdRowHeightAuto
时,是否有办法获取该行的高度

或者,如果有办法得到单元格的高度,我会接受它作为解决方案,因为您可以通过查找行的最大单元格来计算行的高度。

作弊如何

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

我尝试了上面的方法,发现改变HeightRule会改变行的高度,这是因为我试图“冻结”表中显示的高度,这使得上面的方法毫无意义

对于空行或包含一致段落格式的未包装文本的单个段落加上字体大小的行,段落前后的工作方式如下:

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
设置r=c.行
带r
如果.HeightRule wdRowHeightExactly那么
.HeightRule=WDrowHeightXactly
设置p=c.Range.ParagraphFormat
.Height=c.BottomPadding+c.TopPadding+p.SpaceBefore+p.SpaceAfter+p.linespace
如果结束
以

可以使用Range.Information()查找行高。以下代码段不适用于表中的最后一行或页面上的最后一行

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)
这将通过计算选定行与下一行之间的位置差,以点为单位返回行的高度

我有一个例程,它在所有情况下都能工作,并返回单元格中第二行和后续行的点高度,即单行单元格返回0。(我在一个应用程序中使用它,该应用程序会减小某些单元格中的字体大小,以使文本适合一行。)


这适用于简单的文档。不过,在我尝试获取高度的过程中,还有其他处理过程。因此,我正在寻找一种解决方案,它不涉及以任何方式更改文档。找不到任何更好的方法来做到这一点。
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)