I am using a script in Word to find every table with 3 columns, and then set their width accordingly. Not every table is this format, so I can’t run a generic script. These tables have a question at the top that is a merged row of the cells, and then the 3 columns below have the answers and percentage breakdowns. If the question at the top is only 1 line then the script works. If the question is more that 1 line then the table columns are resized, but they are not the proper width (but they are close) .
BUT if I edit the table properties of one of the incorrect tables, the width value shown is correct. I just have to click on the ‘Next Column’ dialog button–without changing anything–and the columns magically pop into alignment.
I’m assuming this is a bug in Word, so my question is does anyone have another way to perform this task that might work better? My reports have about 500 tables in them, and about 450 of them are the 3-column tables I have to adjust, so automation would be so helpful!
This is the script I am using:
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
pgrisham is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.