PDA

View Full Version : Solved: Using AutoFit on a table



will1128
08-31-2010, 03:12 PM
I've created a button to draw a table. How do I make sure the contents inside the table fit to the table? Combo boxes are causing the problem.

Here's my code:


Public Sub AddScheduleButton_Click()
Set myRange = ActiveDocument.Range(Start:=0, End:=0)

ActiveDocument.Tables.Add Range:=myRange, NumRows:=3, NumColumns:= _
8, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed

ActiveDocument.Tables(1).Cell(2, 1).Range.Text = "Start Time"
ActiveDocument.Tables(1).Cell(3, 1).Range.Text = "End Time"
ActiveDocument.Tables(1).Cell(1, 2).Range.Text = "Sunday"
ActiveDocument.Tables(1).Cell(1, 3).Range.Text = "Monday"
ActiveDocument.Tables(1).Cell(1, 4).Range.Text = "Tuesday"
ActiveDocument.Tables(1).Cell(1, 5).Range.Text = "Wednesday"
ActiveDocument.Tables(1).Cell(1, 6).Range.Text = "Thursday"
ActiveDocument.Tables(1).Cell(1, 7).Range.Text = "Friday"
ActiveDocument.Tables(1).Cell(1, 8).Range.Text = "Saturday"

Dim i As Integer

For i = 2 To 8
'populate 1st and 2nd row with combo boxes
ActiveDocument.Tables(1).Cell(2, i).Select
Selection.InlineShapes.AddOLEControl ClassType:="Forms.ComboBox.1"
ActiveDocument.Tables(1).Cell(3, i).Select
Selection.InlineShapes.AddOLEControl ClassType:="Forms.ComboBox.1"
Debug.Print i
Next i

ActiveDocument.Tables(1).AutoFitBehavior wdAutoFitContent
Debug.Print myRange
End Sub



Edited 1-Sep-10 by geekgirlau. Reason: insert vba tags

Tinbendr
08-31-2010, 07:05 PM
Changed the table behavior in the Add method.

Public Sub AddScheduleButton_Click()
Set myRange = ActiveDocument.Range(Start:=0, End:=0)

ActiveDocument.Tables.Add Range:=myRange, NumRows:=3, NumColumns:= _
8, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitContent

With ActiveDocument.Tables(1)
.Cell(2, 1).Range.Text = "Start Time"
.Cell(3, 1).Range.Text = "End Time"
.Cell(1, 2).Range.Text = "Sunday"
.Cell(1, 3).Range.Text = "Monday"
.Cell(1, 4).Range.Text = "Tuesday"
.Cell(1, 5).Range.Text = "Wednesday"
.Cell(1, 6).Range.Text = "Thursday"
.Cell(1, 7).Range.Text = "Friday"
.Cell(1, 8).Range.Text = "Saturday"

Dim i As Integer

For i = 2 To 8
'populate 1st and 2nd row with combo boxes
With .Cell(2, i).Range
.InlineShapes.AddOLEControl ClassType:="Forms.ComboBox.1"
'Set the width of the combobox
.InlineShapes(.InlineShapes.Count).Width = 50
.InlineShapes(.InlineShapes.Count).OLEFormat.Object.List = _
Array("00:00", "01:00", "02:00", "03:00", "04:00", _
"05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", _
"13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", _
"21:00", "22:00", "23:00", "24:00")

End With

With .Cell(3, i).Range
.InlineShapes.AddOLEControl ClassType:="Forms.ComboBox.1"
.InlineShapes(.InlineShapes.Count).Width = 50
.InlineShapes(.InlineShapes.Count).OLEFormat.Object.List = _
Array("00:00", "01:00", "02:00", "03:00", "04:00", _
"05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", _
"13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", _
"21:00", "22:00", "23:00", "24:00")

End With

Debug.Print i
Next i
End With
End Sub


For inquiring minds.

This thread is part of this thread.

Another approach would be to create a few extra rows of scheduling, then Hide the unneeded rows until called for.

will1128
09-01-2010, 06:25 AM
Thank you very much for your help and illustrating how to populate the combo box. This works wonderfully!!!!

fumei
09-01-2010, 12:49 PM
Would it not be better to define an array (Times) with the times and then use the defined array twice - rather than building the array twice?