PDA

View Full Version : Solved: Table Modifications



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

macropod
12-25-2012, 11:59 PM
Hi Greg,

I think you're on the right path, though the code could be tweaked a bit:
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)
With .Cells
'Convert basic 7 column row to a 3 column row.
.Merge
'Get the composite row width
lngWidth = .Width
.Split 1, 3
'Set the nominal cell width
.Width = 100
End With
'Restore row width by resizing cell 2 width.
.Cells(2).Width = lngWidth - 200
End With
End With
End Sub

gmaxey
12-26-2012, 02:26 AM
Paul,

Thanks. I'll make those changes. I can just merge and split (eliminate the resizing) and then go in with the UI and drag the cell 1 right edge to the left and cell 2 gets wider by a corresponding amount, I can then drag cell 3 left edge to the right and again cell 2 get wider by a corresponding amount.

Other than what you suggest, I suppose there is no way to replicate that behaviour via code?

macropod
12-26-2012, 03:00 AM
Hi Greg,

AFAIK, the drag function is a GUI-only affair. I don't know of an equivalent vba method.

gmaxey
12-26-2012, 03:09 AM
Paul,

That makes two of us.