为什么我的设置表格列宽的脚本不能一直工作?

问题描述 投票:0回答:1

我正在 Word 中使用脚本来查找每个包含 3 列的表格,然后相应地设置它们的宽度。并非每个表都是这种格式,因此我无法运行通用脚本。这些表格的顶部有一个问题,是单元格的合并行,然后下面的 3 列有答案和百分比细分。如果顶部的问题只有 1 行,则脚本有效。如果问题超过 1 行,则调整表格列的大小,但它们的宽度不正确(但它们很接近)。

但是如果我编辑不正确表格之一的表格属性,则显示的宽度值是正确的。我只需单击“下一列”对话框按钮(无需更改任何内容),各列就会神奇地弹出并对齐。

我假设这是 Word 中的一个错误,所以我的问题是是否有人有另一种方法来执行此任务,可能效果更好?我的报告中有大约 500 个表格,其中大约 450 个是我必须调整的 3 列表,所以自动化会很有帮助!

这是我正在使用的脚本:

Sub SetTableWidthStrictly()
    Dim tbl As Table
    Dim tableRow As Row
    
    ' Loop through each table in the document
    For Each tbl In ActiveDocument.Tables
        ' Check if the table has three columns
        If tbl.Columns.Count = 3 Then
            ' Loop through each row in the table
            For Each tableRow In tbl.Rows
                If tableRow.Cells.Count = 3 Then
                   ' Set the width of each cell strictly
                   tableRow.Cells(1).PreferredWidthType = wdPreferredWidthPoints
                   tableRow.Cells(1).PreferredWidth = 332
                   
                   tableRow.Cells(2).PreferredWidthType = wdPreferredWidthPoints
                   tableRow.Cells(2).PreferredWidth = 55
                   
                   tableRow.Cells(3).PreferredWidthType = wdPreferredWidthPoints
                   tableRow.Cells(3).PreferredWidth = 81
                End If
            Next tableRow
        End If
    Next tbl
End Sub
ms-word
1个回答
0
投票

尝试添加:

tbl.AllowAutoFit = False

这是为了禁用:

Automatically Resize to fit contents

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.