vba 使用 word 宏仅更改三列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12101814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:27:10  来源:igfitidea点击:

Use word macro to change three column tables only

vbams-wordword-2010

提问by nicemanda

The following is an excerpt from a macro I am currently using to reformat large documents in word to our company branding.

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

    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)

The documents that I get have now been coming through with three column tables as well as two column tables and I would like these to have all columns 5.46 wide, whereas I need the two column tables to stick with the widths I have specified above to keep all the formatting looking good.

我现在得到的文件已经通过三个列表和两个列表来完成,我希望这些列的所有列宽 5.46,而我需要两个列表坚持我在上面指定的宽度以保持所有格式看起来都不错。

I wanted to put in an "if, then" type statement that says if the table has three columns do this, if the table has two then do this but I don't know how to identify 3 column tables from 2 column tables.

我想放入一个“if, then”类型的语句,表示如果表有三列,则执行此操作,如果表有两列,则执行此操作,但我不知道如何从 2 列表中识别 3 列表。

回答by Tim Williams

EDIT: updated to deal with rows having uneven column widths.

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

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