nicemanda
nicemanda

Reputation: 91

Use word macro to change three column tables only

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.

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.

Upvotes: 3

Views: 6141

Answers (1)

Tim Williams
Tim Williams

Reputation: 166790

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

Upvotes: 5

Related Questions