【问题标题】:Use word macro to change three column tables only仅使用 word 宏更改三列表
【发布时间】:2015-03-01 08:06:13
【问题描述】:

以下是我目前用于将大型文档重新格式化为我们公司品牌的宏的摘录。

    Selection.Tables(1).Select
    Selection.Columns(1).Width = CentimetersToPoints(5.46)
    Selection.Columns(2).Width = CentimetersToPoints(10.92)
    Selection.Rows.HeightRule = wdRowHeightAtLeast
    Selection.Rows.Height = CentimetersToPoints(0.8)

我现在得到的文档已经包含三个列表和两个列表,我希望这些列的所有列都是 5.46 宽,而我需要两个列表保持我指定的宽度以保持所有格式看起来不错。

我想输入一个“如果,那么”类型的语句,说明如果表有三列则执行此操作,如果表有两列则执行此操作,但我不知道如何从 2 列中识别 3 列表表格。

【问题讨论】:

    标签: vba ms-word word-2010


    【解决方案1】:

    编辑:更新以处理列宽不均匀的行。

    Dim tbl As Table
    Dim rw As Row
    
    Set tbl = ActiveDocument.Tables(1)
    
    For Each rw In tbl.Rows
    With rw
    
        .Cells(1).Width = CentimetersToPoints(5.46)
    
        If .Cells.Count = 2 Then
            .Cells(2).Width = CentimetersToPoints(10.92)
        ElseIf .Cells.Count = 3 Then
            .Cells(2).Width = CentimetersToPoints(5.46)
            .Cells(3).Width = CentimetersToPoints(5.46)
        Else
            'what do do if not 2 or 3?
        End If
    
        .HeightRule = wdRowHeightAtLeast
        .Height = CentimetersToPoints(0.8)
    End With
    Next rw
    

    【讨论】:

    • 太令人沮丧了——这个答案正是我所需要的,但是当我运行它时,它说Cannot access individual columns in this collection because the table has mixed cell widths :(
    • 我认为这告诉您您的表格在不同的行上具有不同的列宽,因此您可能必须遍历行并逐个单元格设置列宽。不是一个大字 VBA 用户,所以恐怕这已接近我的知识极限....
    • @nicemanda:这个解决方案只有在你有homogeneous的表时才有效,无论是按行还是按列。可能表格中的某些单元格被合并,导致显示错误。
    • 查看更新 - 应该处理不均匀的单元格宽度甚至合并单元格,但您可能需要对其进行定制以适合您的确切布局...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2012-11-18
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多