I would like to do the something very similar to what is done in the answer given to this post. The only difference is that I would like to add the OLEObjects to a userform, not to a worksheet.
EDIT 1 : I have tried UserForm1.OLEObjects.Add("Forms.ComboBox.1")
EDIT 2 : I post the entire code here :
'this is public so it doesn't go out of scope
Public gclsControlEvents As CControlEvents
Sub Bouton1_Click()
Call MakeCombo
End Sub
Sub MakeCombo()
Dim oleCbx As OLEObject
'Create the combobox
Set oleCbx = UserForm1.Controls.Add("Forms.ComboBox.1") 'Bug at this line
oleCbx.Object.AddItem "1"
oleCbx.Object.AddItem "2"
'hookup the events
Application.OnTime Now, "HookupEvents"
End Sub
Sub HookupEvents()
Set gclsControlEvents = New CControlEvents
Set gclsControlEvents.Cbx = UserForm1.OLEObjects(1).Object
End Sub
EDIT 3 : Here is the class code :
Private WithEvents mclsCbx As MSForms.ComboBox
Public Property Set Cbx(ByVal clsCbx As MSForms.ComboBox): Set mclsCbx = clsCbx: End Property
Public Property Get Cbx() As MSForms.ComboBox: Set Cbx = mclsCbx: End Property
Private Sub mclsCbx_Change()
MsgBox Me.Cbx.Name
End Sub