PDA

View Full Version : Table column widths



ldoodle
09-11-2017, 03:00 AM
Hey,

Given this code, in Outlook 2010 (is fine in 2016?!) why would it error out on the lines setting the PreferredWidth of each column:


Set Selection = Inspector.WordEditor.Application.Selection
With Selection
If .Tables.Count < 1 Then

Set Table = .Tables.Add(Range:=.Range, NumRows:=1, NumColumns:=4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:=wdAutoFitFixed)
.InsertRows NumRows:=FileCount

With Table

.Style = "Table Grid"
.PreferredWidthType = wdPreferredWidthPoints
.PreferredWidth = 700
.TopPadding = CentimetersToPoints(0.12)
.BottomPadding = CentimetersToPoints(0.12)
.Columns.PreferredWidthType = wdPreferredWidthPoints
----> .Columns(1).PreferredWidth = 30 <----
----> .Columns(2).PreferredWidth = 470 <----
----> .Columns(3).PreferredWidth = 90 <----
----> .Columns(4).PreferredWidth = 110 <----

With .Borders

.Enable = True
.OutsideColor = 5263440
.InsideColor = 5263440

End With

With .Rows.First

.HeadingFormat = True
.Height = 20
.Shading.BackgroundPatternColor = ColorPicker()
.Cells.VerticalAlignment = wdCellAlignVerticalCenter
.Cells(1).Range.Text = "No."
.Cells(2).Range.Text = "Name"
.Cells(3).Range.Text = "Size"
.Cells(4).Range.Text = "Expiry Date"

With .Range

.Font.Color = 16777215
.Font.Size = 12

End With

End With

End With

Else

Set Table = .Tables(1)
.InsertRows NumRows:=FileCount

End If

End With


The error is "5992: Cannot access individual columns in this collection because the table has
mixed cell widths." I understand this to be because there a merged cells, but there aren't any in my table - it's a brand new table each time.

The exact same code seems to work OK directly in Word VBA editor.

Thanks

gmayor
09-11-2017, 05:24 AM
I am surprised it works at all as there are some lines that do not accord with the Word VBA syntax that you are attempting to use and others that are not required at all (and frankly I wouldn't have used the variable name Table to avoid confusion with the built in Table function). The following, bearing in mind that you have not posted the whole of the code, might be closer:


With Selection
If .Tables.Count < 1 Then
Set oTable = .Tables.Add(Range:=.Range, _
NumRows:=1 + FileCount, NumColumns:=4, _
DefaultTableBehavior:=1, _
AutoFitBehavior:=0)
With oTable
.Style = "Table Grid"
.TopPadding = CentimetersToPoints(0.12)
.BottomPadding = CentimetersToPoints(0.12)
.Columns(1).Width = 30
.Columns(2).Width = 470
.Columns(3).Width = 90
.Columns(4).Width = 110

With .Borders
.Enable = True
.OutsideColor = 5263440
.InsideColor = 5263440
End With

With .Rows.First
.HeadingFormat = True
.Height = 20
.Shading.BackgroundPatternColor = &HBD814F
.Cells.VerticalAlignment = 1
.Cells(1).Range.Text = "No."
.Cells(2).Range.Text = "Name"
.Cells(3).Range.Text = "Size"
.Cells(4).Range.Text = "Expiry Date"
With .Range
.Font.Color = 16777215
.Font.Size = 12
End With
End With
End With
Else
Set oTable = .Tables(1)
For i = 1 To FileCount
Table.Rows.Add
Next i
End If
End With