通过 VBA 设置 table AutoFit 与单击 UI 按钮给出不同的结果

Setting table AutoFit via VBA gives different result than clicking UI button

我正在尝试设置 Word table 的 AutoFitBehavior 以适应内容和 window - 单击 'AutoFit Contents' 按钮,然后 'AutoFit Window' 给出我想要的结果要得到。问题是,当我使用 VBA 执行此操作时,格式不同。有趣的是,当我 运行 宏一步一步 (F8) 时,它给出了预期的结果(与 UI 相同)。

这是我的代码:

Documents(1).Activate
With ActiveDocument.Tables(2)
    .AllowAutoFit = True
    .AutoFitBehavior 1
    .AutoFitBehavior 2
End With

如您所见,它非常简单 - 我找不到任何不正常工作的原因。

我也不认为这个问题与使用 'ActiveDocument' 属性 有关,因为在完整的宏中,这段代码直接在新创建的命名文档上执行,所以我确保它在正确的文件中寻址正确的 table。

我知道我可以使用 PreferredWidth 属性 设置列宽,但使用 AutoFit 会简单得多,因为我并不总是知道我的数据的长度。

有没有办法让这个方法像从 UI 调用时一样工作?


编辑: 根据 Cindy Meister 的要求,我添加了我正在使用的实际代码的片段:

Set wordApp = CreateObject("Word.Application")
Set wordDoc = wordApp.Documents.Add(strPath)

With wordDoc
    .Tables.Add Range:=wordDoc.Bookmarks("tableBookmark").Range, NumRows:=licenceRows, NumColumns:=3

    '[omitted: populating the table]

    .Tables(1).Split(splitRow)
    With .Tables(2)
        .Range.Collapse Direction:=0
        .Range.InsertBreak Type:=7
        .AllowAutoFit = True
        .AutoFitBehavior 1
        .AutoFitBehavior 2
    End With
End With

它是从我用来从模板创建报告文件的 Excel 宏中调用的。我正在使用 Office 2013。

我今天还注意到另一件事:当我设置 wordApp.Visible = True 时,滚动到 table 并从字面上查看方法的工作方式 - 它的格式正确。就像 Word 应用程序无法正确使用此方法,直到它 必须 向您展示每个步骤(与逐步 运行 一样)。

感谢您提供更多信息。对于未来,在这种情况下,如果您也包括来自 Excel 的 运行 的自动化代码将会有所帮助,因为问题可能与该接口有关...

我 运行 在 Office 2013(以及 2010)中使用以下代码,它按预期工作:table 适合页面宽度(边距到边距)和单元格展开以适合内容。

与您所拥有的相反,我在 Tables.Add 中使用了可选参数来设置默认行为以在创建 table 时允许自动调整。然后我不需要设置所有这些 after-the-fact.

另请注意我对 tables 和范围使用对象变量,释放对象并更新屏幕。

'Table should fit the page and fit the contents
Sub TestFormatTableStructure()
    Dim wordApp As Word.Application
    Dim wordDoc As Word.Document
    Dim tbl1 As Word.Table, tbl2 As Word.Table
    Dim rngTbl As Word.Range

    On Error GoTo ErrHandler
    Set wordApp = New Word.Application
    Set wordDoc = wordApp.Documents.Add

    wordApp.ScreenUpdating = False
    With wordDoc
        Set tbl1 = .Tables.Add(Range:=wordDoc.Paragraphs.Last.Range, _
                   NumRows:=6, NumColumns:=3, _
                   DefaultTableBehavior:=wdWord9TableBehavior, _
                   AutoFitBehavior:=2)

    '[omitted: populating the table]

    Set tbl2 = tbl1.Split(4)
        With tbl2
            Set rngTbl = .Range
            rngTbl.Collapse Direction:=0
            rngTbl.InsertBreak Type:=7
            '.AllowAutoFit = True
            '.AutoFitBehavior 1
            .AutoFitBehavior 2
        End With
    End With

ErrHandler:
    wordApp.Visible = True
    wordApp.ScreenUpdating = True
    Set tbl1 = Nothing
    Set tbl2 = Nothing
    Set rngtlb = Nothing
    Set wordDoc = Nothing
    Set wordApp = Nothing
End Sub

感谢 and following 我意识到了我的错误 - 我认为自动调整会使列适合任何文本,包括带有 line-breaking 个字符(如空格)的文本。出来它不是那样工作的。

最后,为了按照我的意愿格式化 table(window-wide table,列适合内容)我使用了以下代码:

'Table should fit the page and fit the contents
Sub TestFormatTableStructure()
Dim wordApp As Word.Application
Dim wordDoc As Word.Document
Dim tbl1 As Word.Table, tbl2 As Word.Table

On Error GoTo ErrHandler
Set wordApp = New Word.Application
Set wordDoc = wordApp.Documents.Add

wordApp.ScreenUpdating = False

With wordDoc
    Set tbl1 = .Tables.Add(Range:=wordDoc.Paragraphs.Last.Range, _
               NumRows:=6, NumColumns:=3, _
               DefaultTableBehavior:=wdWord9TableBehavior, _
               AutoFitBehavior:=wdAutoFitContent) 'autofit content
    With tbl1
        '[omitted: populating the table]
        .PreferredWidthType = wdPreferredWidthPercent
        .PreferredWidth = 100
    End With

    Set tbl2 = tbl1.Split(4)
    'dont have to set formatting again for second table, its inherited
    With tbl2
        '[do things]
    End With
End With

ErrHandler:
    wordApp.Visible = True
    wordApp.ScreenUpdating = True
    Set tbl1 = Nothing
    Set tbl2 = Nothing
    Set rngtlb = Nothing
    Set wordDoc = Nothing
    Set wordApp = Nothing
End Sub