【问题标题】:How to make a table fit the window height in Word?如何使表格适合Word中的窗口高度?
【发布时间】:2020-10-14 16:30:43
【问题描述】:

我是 VBA 新手。我有一个有 4 列和近 2000 行的表,它以 3 列 Word 布局显示。我的问题是 3 列的最后一行并不总是在一个高度上,如图所示(用单元格中的白色字体掩盖了个人数据)。 [餐桌图片1

我认为一个解决方案是将具有 2 行的行的高度设置为 0.54 厘米,将 1 行的行设置为 0.27 厘米。我手动完成了这项工作,并且奏效了。我正在寻找一个宏来实现这一点。下面的代码将提供对我正在尝试做的事情的理解。

Sub height ()

ActiveDocument.Tables(1).Rows.HeightRule = wdRowHeightAuto
     'this automatically sets the 2-line rows to 0.53cm and the 1-line rows to 0.25cm

For Each row in Rows
   If Row.RowHeight < 0.5cm Then 
      Row.RowHeight = 0.27cm
   Else Row.RowHeight = 0.54cm
   End If

Next Row

End Sub

我知道这段代码不能工作,但我认为它向你展示了我想要做什么。

【问题讨论】:

  • Row.RowHeight 不会返回有用的值,除非该行已设置为精确的高度。您最好更改文本的行距,以便它产生您想要的单元格高度。

标签: vba ms-word


【解决方案1】:

由于Row.RowHeight 不会返回有用的值,除非行已设置为精确的高度,您可以更改文本的行距,以便自动达到所需的单元格高度。

下面的代码假定您要调整的表格是文档中的第一个表格。

Sub AdjustLineSpacingForTable()
   Dim reqdLineSpacing As Single
   'start with the height of a double height cell
   reqdLineSpacing = CentimetersToPoints(0.54)
   With ActiveDocument.Tables(1)
      'table cells have padding top and bottom so we need to subtract that from the required height
      reqdLineSpacing = reqdLineSpacing - (.TopPadding + .BottomPadding)
      'now we can adjust the text spacing
      With .Range.ParagraphFormat
         'ensure there is no additional spacing on the paragraphs
         .SpaceAfter = 0
         .SpaceBefore = 0
         'set line spacing to an exact amount
         .LineSpacingRule = wdLineSpaceExactly
         .LineSpacing = reqdLineSpacing / 2
      End With
   End With
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 2011-07-16
    • 1970-01-01
    相关资源
    最近更新 更多