PDA

View Full Version : Word 2010 Macro - Populating ComboBox on Open Event



eddie1106
04-12-2012, 05:21 AM
Hi,

I've searched in the entire internet about this and I didn't find any solution for my problem.

I have a form containing several comboboxes and they have to be populate with 4 items. I want to populate them in the Document_Open Event, so I started with:
Private Sub Document_Open ()
And now begins my problem, How can I loop for all combobox in the document to add items? I thought in use a For Each / Next structure, but I can't define what kind of object a combobox is. I read in some forums that in Word 2010 they are located inside de InLineShapes collection, but I couldn't make any code to work.

Is there anybody that can help me??

Thanks a lot!!!

Eddie

fumei
04-12-2012, 05:08 PM
"I've searched in the entire internet about this"

The ENTIRE Internet? I seriously doubt that. I know I have posted on this on a couple of occasions.

IF the comboboxes are the only InlineShapes, and IF each gets the SAME values, the following populates all of them with the same values on document open.Option Explicit

Private Sub Document_Open()
Call PopulateCB
End Sub

Sub PopulateCB()
Dim objCombo As InlineShape
For Each objCombo In ActiveDocument.InlineShapes
objCombo.OLEFormat.Object.AddItem "blah"
objCombo.OLEFormat.Object.AddItem "yadda"
objCombo.OLEFormat.Object.AddItem "whatever"
objCombo.OLEFormat.Object.AddItem "ho-hum"
objCombo.OLEFormat.Object.ListIndex = 0
Next
End SubNote that it also makes the first item the visible one (ListIndex = 0).

eddie1106
04-13-2012, 08:48 AM
Worked Perfectly!!!!

Thank you!!!!

I removed the "ListIndex = 0", because I want to maintain the last saved selection.

fumei
04-13-2012, 02:08 PM
Oh really. Do tell. How are you going to do that? This is on document open. There will be no saved selection. You can select whatever you want...this will not be what shows on document open.

eddie1106
04-16-2012, 05:21 AM
Hi,

In the document open procedure we are going to add items in the listbox.

the Combobox.Text is kept when you save the file. If you don't populate then when you open the file, you have the last saved selection but you can't select any other option.

Try it.

aksaid2000
05-31-2017, 02:26 PM
"I've searched in the entire internet about this"

The ENTIRE Internet? I seriously doubt that. I know I have posted on this on a couple of occasions.

IF the comboboxes are the only InlineShapes, and IF each gets the SAME values, the following populates all of them with the same values on document open.Option Explicit

Private Sub Document_Open()
Call PopulateCB
End Sub

Sub PopulateCB()
Dim objCombo As InlineShape
For Each objCombo In ActiveDocument.InlineShapes
objCombo.OLEFormat.Object.AddItem "blah"
objCombo.OLEFormat.Object.AddItem "yadda"
objCombo.OLEFormat.Object.AddItem "whatever"
objCombo.OLEFormat.Object.AddItem "ho-hum"
objCombo.OLEFormat.Object.ListIndex = 0
Next
End SubNote that it also makes the first item the visible one (ListIndex = 0).


Hello,
i know its perhaps an old post but i realy nead help in this macro.Well my problem is that i need to open a word document containing a table.i have a combobox in the first column and another one in the third. the probleme is that they have different items.do you have an idea about this? with my advanced thanks.

gmaxey
05-31-2017, 05:16 PM
Unfortunately at this point I don't have any idea what you want to do with either combobox. What king of comboboxes are they?

aksaid2000
06-01-2017, 05:21 AM
Unfortunately at this point I don't have any idea what you want to do with either combobox. What king of comboboxes are they?

Hello, i want comboboxes of the type: ClassType = "Forms.ComboBox.1" and i want that on each row of the column 1 and threee will be a combobox.this is the general code:
'Private Sub Document_Open()
'Dim cbx As ComboBox, ishp As InlineShape
'ThisDocument.Tables(1).Columns(1).Select
'For Each ishp In Selection.InlineShapes
'If ishp.Type = wdInlineShapeOLEControlObject Then
'If ishp.OLEFormat.ClassType = "Forms.ComboBox.1" Then
'Set cbx = ishp.OLEFormat.Object
'cbx.AddItem "Saturday"
'cbx.AddItem "Sunday"
'cbx.AddItem "monday"
....etc
'End If
'End If
'Next
'ThisDocument.Tables(1).Columns(3).Select
'For Each ishp In Selection.InlineShapes
'If ishp.Type = wdInlineShapeOLEControlObject Then
'If ishp.OLEFormat.ClassType = "Forms.ComboBox.1" Then
'Set cbx = ishp.OLEFormat.Object
''cbx.Clear
'cbx.AddItem "January"
'cbx.AddItem "Febuary"
...etc
'End If
'End If
'Next
'End Sub
Unfortunately i do not get the good result.i hope this will give you an idea about what i want.thank you

aksaid2000
06-01-2017, 05:23 AM
i just want to add that the comboboxes are inserted in the document.i just want the items to be loaded.

gmaxey
06-01-2017, 07:02 AM
Private Sub Document_Open()
Dim cbx As ComboBox, ishp As InlineShape
Application.ScreenUpdating = False
For Each ishp In ActiveDocument.Tables(1).Range.InlineShapes
ishp.Select
If ishp.Type = wdInlineShapeOLEControlObject Then
If ishp.OLEFormat.ClassType = "Forms.ComboBox.1" Then
Set cbx = ishp.OLEFormat.Object
cbx.Clear
Select Case ishp.Range.Information(wdEndOfRangeColumnNumber)
Case 1:
cbx.AddItem "Sunday"
cbx.AddItem "Monday"
Case 3:
cbx.AddItem "January"
cbx.AddItem "February"
End Select
End If
End If
Next
Application.ScreenUpdating = True
End Sub

aksaid2000
06-01-2017, 02:31 PM
Private Sub Document_Open()
Dim cbx As ComboBox, ishp As InlineShape
Application.ScreenUpdating = False
For Each ishp In ActiveDocument.Tables(1).Range.InlineShapes
ishp.Select
If ishp.Type = wdInlineShapeOLEControlObject Then
If ishp.OLEFormat.ClassType = "Forms.ComboBox.1" Then
Set cbx = ishp.OLEFormat.Object
cbx.Clear
Select Case ishp.Range.Information(wdEndOfRangeColumnNumber)
Case 1:
cbx.AddItem "Sunday"
cbx.AddItem "Monday"
Case 3:
cbx.AddItem "January"
cbx.AddItem "February"
End Select
End If
End If
Next
Application.ScreenUpdating = True
End Sub


I thank you very much for the answer,it's very interesting!!!
i tried it in a simple table and it worked perfectly,but when i tried it in my particular table where there are some fusion of the above lines of the table,the comboboxes seems to be blocked until i click on a line then i activate and desactivate the creation mode.
do you have an idea about how to get around this special case? thank you again for the precedent code you are a genius!!!!

aksaid2000
06-01-2017, 03:08 PM
oh! very nice i tried the code in other computer and all thing is fine.the probleme is perhaps in my computer.Thank you very much Greg!!!!!!!!!!

gmaxey
06-01-2017, 04:08 PM
No, but I also experience something that I could quite figure out. If the shape is not selected i.g., ishp.Select then the code would not correctly determine the column index.