Log in

View Full Version : [SOLVED:] Adjust Cell width but not affect overal row width



gmaxey
06-02-2020, 08:45 AM
Tables can drive me to the point of insanity.

If I have a four row x three column table in a document, I can select one of the column 2 cells and adjust its width without affecting the width of the overall row. If I try that with VBA the overall row size is changed.


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table
Set oTbl = ActiveDocument.Tables.Add(Selection.Range, 4, 3, wdWord8TableBehavior, wdAutoFitWindow)
oTbl.Range.Cells(5).Width = InchesToPoints(1)
lbl_Exit:
Exit Sub
End Sub

I can work around that with what I feel is a cumbersome process:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table
Dim dblW As Double
Set oTbl = ActiveDocument.Tables.Add(Selection.Range, 4, 3, wdWord8TableBehavior, wdAutoFitWindow)
'Option A
oTbl.Range.Cells(5).Width = InchesToPoints(1)
'at this point the overall row not longer is fit to window.
'Option B
dblW = oTbl.Range.Cells(5).Width + oTbl.Range.Cells(6).Width
oTbl.Range.Cells(5).Width = InchesToPoints(1)
oTbl.Range.Cells(6).Width = dblW - oTbl.Range.Cells(5).Width
lbl_Exit:
Exit Sub
End Sub

Is there some setting that can be applied to the table (I thought wdAutoFitWindow) so that regardless of what you do to individual cells (provided you don't try something illogical) that the overall table row remains the same width?

SamT
06-04-2020, 06:33 PM
:dunno PreferredWidthType and PreferredWidth? Supposedly applies to Cells and Tables

Paul_Hossler
06-07-2020, 07:05 PM
I think this is what you were looking for




Option Explicit


Sub ScratchMacro_phh()
Dim oTable As Table
Dim numRows As Long, numCols As Long
Dim numRow As Long, numCol As Long

'delete existing table for development
On Error Resume Next
ActiveDocument.Tables(1).Delete
On Error GoTo 0

'size
numRows = 4
numCols = 3

Set oTable = ActiveDocument.Tables.Add(Selection.Range, numRows, numCols, wdWord8TableBehavior, wdAutoFitWindow)

'cell to change
numRow = 2
numCol = 2

Call oTable.Range.Rows(numRow).Cells(numCol).SetWidth(InchesToPoints(1), wdAdjustProportional)
End Sub

macropod
06-07-2020, 07:12 PM
Perhaps:

Sub Demo()
Dim oTbl As Table
Set oTbl = ActiveDocument.Tables.Add(Selection.Range, 4, 3, wdWord8TableBehavior, wdAutoFitWindow)
oTbl.Range.Cells(5).SetWidth InchesToPoints(1), wdAdjustProportional
End Sub

gmaxey
06-08-2020, 05:24 AM
Paul H thanks. Exactly.

Break

Paul E
Yes, our coding style is often similar.

Paul_Hossler
06-08-2020, 07:21 AM
Yea, I tend to be wordy :yes

I also tend to think of Word tables as an Excel worksheet with rows and columns



Don't forget to mark this one [SOLVED]