Word中如何获取表格行高

发布于 2024-10-10 14:20:28 字数 219 浏览 7 评论 0原文

我到处寻找并尝试了各种方法。一直认为这是不可能完成的。所以我要在这里尝试一下,看看其他人是否有运气。

当行的 HeightRule 设置为 wdRowHeightAuto 时,有什么方法可以获取 Word 中表格行的高度吗?

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋凉 2024-10-17 14:20:28

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

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)
心的憧憬 2024-10-17 14:20:28

作弊怎么办?

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

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
月下伊人醉 2024-10-17 14:20:28

我尝试了上面的操作,发现更改 HeightRule 会更改行的高度,鉴于我试图“冻结”事先出现在表格中的高度,这使得上面的内容毫无意义。

对于空行或包含以一致段落格式添加字体大小的单个展开文本段落的行,para before 和 after 可以按如下方式工作:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文