如何用vba调整word表格的行高、列宽?

word表格的行高、列宽既可以单独设置,也可以全部一次性设置为统一的值。

要在vba中单独的调整word表格的行高、列宽,可以使用如下的vba代码:

Sub exceloffice()
    '作者QQ:1722187970,微信:xycgenius,微信公众号exceloffice
    Dim oDoc As Document
    Set oDoc = Word.ActiveDocument
    Dim oT As Table
    Dim oRow As Row
    Dim oColumn As Column
    With oDoc
            Set oT = .Tables(1)
            With oT
                For Each oRow In .Rows
                    oRow.Height = 20
                Next
                For Each oColumn In .Columns
                    oColumn.Width = 110
                Next
            End With
    End With
End Sub

如果要一次就设置所有行高或者列宽为某个值,可以使用如下的vba代码;

Sub exceloffice()
    '作者QQ:1722187970,微信:xycgenius,微信公众号exceloffice
    Dim oDoc As Document
    Set oDoc = Word.ActiveDocument
    Dim oT As Table
    Dim oRow As Row
    Dim oColumn As Column
    With oDoc
            Set oT = .Tables(1)
            With oT
               .Rows.Height = 50
               .Columns.Width = 20
            End With
    End With
End Sub

如果要使得某个表格的所有列宽平均分窗口宽度,可以使用DistributeWidth方法,代码如下:

Sub exceloffice()
    '作者QQ:1722187970,微信:xycgenius,微信公众号exceloffice
    Dim oDoc As Document
    Set oDoc = Word.ActiveDocument
    Dim oT As Table
    Dim oRow As Row
    Dim oColumn As Column
    With oDoc
            Set oT = .Tables(1)
            With oT
                .Columns.DistributeWidth
            End With
    End With
End Sub

如果要使得某个表格的列宽刚刚好够容纳他所在的列的单元格内容,而不要有多余的列宽,则可以使用AutoFit方法,代码如下;

Sub exceloffice()
    '作者QQ:1722187970,微信:xycgenius,微信公众号exceloffice
    Dim oDoc As Document
    Set oDoc = Word.ActiveDocument
    Dim oT As Table
    Dim oRow As Row
    Dim oColumn As Column
    With oDoc
            Set oT = .Tables(1)
            With oT
                .Columns(2).AutoFit
                .Columns.AutoFit
            End With
    End With
End Sub

AutoFit方法不仅可以一次设置所有的列宽自动自适应单元格内容的宽度,又可以单独设置某个列自适应该列的单元格内容的宽度。

但是如果这个表格已经和整个窗口的宽度一样大了,这个方法就无效了。

 

 

 

       

发表评论