gmaxey
12-24-2012, 09:50 AM
I'm basically a rube when it comes to manipulating Word tables (with or without VBA). They often seem to have a mind of their own and thiers can often outwit mine. :(
I need to create a table programatically and then convert the first row from having 7 columns equal width columns to having three columns with unequal widths. I'm using the following code which works, but is there a way to simply modify column 1 and 3 widths after the merge and split while not affecting the overall row width?
E.g.,
Change column 1 width and column 2 width automatically gets wider so the row width still equals the table width.
Change column 3 width and column 2 width again automatically gets wider so the row width still equals the table width.
Or anything else better?
Sub ScratchMacro()
Dim oTbl As Word.Table
Dim lngWidth As Long
Set oTbl = ActiveDocument.Tables.Add(Selection.Range, 8, 7)
With oTbl
With .Rows(1)
'Convert basic 7 column row to a 3 column row.
.Cells.Merge
.Cells.Split 1, 3
'Get the composite row width
lngWidth = .Cells(1).Width + .Cells(2).Width + .Cells(3).Width
'Resize cell 1 & 3 width.
.Cells(1).Width = 100
.Cells(3).Width = 100
'Restore row width by resizing cell 2 width.
.Cells(2).Width = lngWidth - (.Cells(1).Width + .Cells(3).Width)
End With
End With
End Sub
I need to create a table programatically and then convert the first row from having 7 columns equal width columns to having three columns with unequal widths. I'm using the following code which works, but is there a way to simply modify column 1 and 3 widths after the merge and split while not affecting the overall row width?
E.g.,
Change column 1 width and column 2 width automatically gets wider so the row width still equals the table width.
Change column 3 width and column 2 width again automatically gets wider so the row width still equals the table width.
Or anything else better?
Sub ScratchMacro()
Dim oTbl As Word.Table
Dim lngWidth As Long
Set oTbl = ActiveDocument.Tables.Add(Selection.Range, 8, 7)
With oTbl
With .Rows(1)
'Convert basic 7 column row to a 3 column row.
.Cells.Merge
.Cells.Split 1, 3
'Get the composite row width
lngWidth = .Cells(1).Width + .Cells(2).Width + .Cells(3).Width
'Resize cell 1 & 3 width.
.Cells(1).Width = 100
.Cells(3).Width = 100
'Restore row width by resizing cell 2 width.
.Cells(2).Width = lngWidth - (.Cells(1).Width + .Cells(3).Width)
End With
End With
End Sub