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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4630277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:29:29  来源:igfitidea点击:

How to get the height of a table row in Word

vbams-wordheight

提问by Joe

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 HeightRuleis set to wdRowHeightAuto?

当行HeightRule设置为时,有没有办法在 Word 中获取表格行的高度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.

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

采纳答案by Fionnuala

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

回答by grahamj42

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

可以使用 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)

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

我有一个例程适用于所有情况并返回单元格中第二行和后续行的高度(以点为单位),即单行单元格返回 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)

回答by eJulia

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.

我尝试了上述方法,发现更改 HeightRule 会更改行的高度,鉴于我试图预先“冻结”表格中出现的高度,因此上述内容毫无意义。

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