2012-08-24 32 views
3

以下是我正在用于将大文档重新格式化为我们公司品牌的宏的摘录。使用word宏只更改三个列表

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宽的所有列的文件,而我需要两页的表坚持使用我已经在上面指定的宽度,以保持所有的格式看起来不错。

我想放入一个“如果,然后”类型的声明,说如果表有三列做这个,如果表有两个然后做这个,但我不知道如何识别3列表2列表。

回答

5

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

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 
+1

如此令人沮丧 - 这个答案正是我需要的,但是当我运行它,它说,在此集合无法访问个别列,因为表格有混合单元格宽度:( – nicemanda

+0

我认为这是告诉你,你的表有不同行的列宽不同,所以你可能必须循环遍历行并逐列设置列宽。不是一个大的Word VBA用户,所以我恐怕这接近我的知识极限.... –

+0

@nicemanda:这个解决方案只适用于所有表格都是“齐”的表格,不管是按行还是按列。可能表格中的某些单元格会合并,导致显示错误 – Cylian