为文档中的所有表设置列宽

时间:2016-09-02 10:37:31

标签: vba word-vba

我在word文档中有多个表,每个表有6列。我想修复所有表中每列的宽度,我有以下脚本。

它一直给我value out of the range错误。

Sub FormatTables()
    Dim t As Table
    For Each t In ActiveDocument.Tables
        If t.Columns.Count = 6 Then
            t.Range.Select
            Selection.ClearFormatting

            t.AutoFitBehavior _
            wdAutoFitWindow

            't.Rows.DistributeHeight
            t.Rows.WrapAroundText = True
            t.Rows.Height = 0.15

            t.Columns.DistributeWidth

            'tried this way 
            t.Columns(1).Width = 1.5 'value out of the range
            t.Columns(2).Width = 1.5
            t.Columns(3).Width = 6.49
            t.Columns(4).Width = 6.49
            t.Columns(5).Width = 0.8
            t.Columns(6).Width = 0.8

            'as well as this            
            t.Columns(1).SetWidth ColumnWidth:=1.5, _
            RulerStyle:=wdAdjustProportional 'value out of the range
            t.Columns(2).SetWidth ColumnWidth:=1.5, _
            RulerStyle:=wdAdjustProportional
            t.Columns(3).SetWidth ColumnWidth:=6.49, _
            RulerStyle:=wdAdjustProportional
            t.Columns(4).SetWidth ColumnWidth:=6.49, _
            RulerStyle:=wdAdjustProportional
            t.Columns(5).SetWidth ColumnWidth:=0.8, _
            RulerStyle:=wdAdjustProportional
            t.Columns(6).SetWidth ColumnWidth:=0.8, _
            RulerStyle:=wdAdjustProportional

            Debug.Print t.Cell(2, 1).Range.Text
        End If

    Next
End Sub

1 个答案:

答案 0 :(得分:0)

你正试图设置英寸,尝试使用

t.Columns(1).Width = 1.5 * 72 ' 1 inch = 72 points

请参阅MSDN了解.SetWidth函数,因为它调出“width in points”

然后请参阅Word Tips了解点数。