Use a link to a dynamically created control in Word VBA

I am writing a form in Word 2003 to collect several answers to one question. I have a button click macro that duplicates various input fields (pop-ups, radio buttons, etc.) Ready for a new answer.

However, I need to change the text of the radio buttons and set the OnChange event to combobox, and I cannot find the correct syntax for this. Both controls are located on the Control Panel toolbar.

Below is the macro code that should duplicate controls.

 Private Sub CommandButton11_Click() Set Doc = ActiveDocument Response = MsgBox("Add another response?", vbYesNo, "Confirm action") If Response = vbYes Then If Doc.ProtectionType <> wdNoProtection Then Doc.Unprotect End If Selection.MoveRight Selection.MoveDown Selection.TypeParagraph ''# keep the reference to this control and set the OnChange event handler Selection.InlineShapes.AddOLEControl ClassType:="Forms.ComboBox.1" Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.TypeText Text:=vbTab Selection.TypeText Text:=vbTab ''# keep the reference to this control and set text Selection.InlineShapes.AddOLEControl ClassType:="Forms.OptionButton.1" Selection.MoveRight Unit:=wdCharacter, Count:=1 Doc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True End If End Sub 
+4
source share
1 answer

adding event handlers dynamically is a bit complicated.

You can add code dynamically to ThisDocument. This is described by Microsoft: http://support.microsoft.com/?scid=kb%3Ben-us%3B246299&x=14&y=10 . However, when I tried this Word 2007 crashed.

Another way is to add a class to handle events and create an instance of this class for each control. Paste the following code into the module:

 Option Explicit Public objControls() As clsComboBox Private Sub CommandButton11_Click() Dim objShape As InlineShape If ActiveDocument.ProtectionType <> wdNoProtection Then ActiveDocument.Unprotect End If Selection.MoveRight Selection.MoveDown Selection.TypeParagraph ' keep the reference to this control and set the OnChange event handler Set objShape = Selection.InlineShapes.AddOLEControl(ClassType:="Forms.ComboBox.1") With objShape.OLEFormat.Object .AddItem "Item 1" .AddItem "Item 2" End With Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.TypeText Text:=vbTab Selection.TypeText Text:=vbTab ' keep the reference to this control and set text Set objShape = Selection.InlineShapes.AddOLEControl(ClassType:="Forms.OptionButton.1") With objShape.OLEFormat.Object .Caption = "My great option" End With Selection.MoveRight Unit:=wdCharacter, Count:=1 ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True ' we have to execute the creation of the event handlers with a delay ' to make it work (seems Word needs some time for object creation) Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="prcCreateReference" End Sub Public Sub prcCreateReference() Dim objShape As InlineShape Dim intCount As Integer On Error Resume Next For Each objShape In ThisDocument.InlineShapes intCount = intCount + 1 ReDim Preserve objControls(1 To intCount) If TypeOf objShape.OLEFormat.Object Is ComboBox Then Set objControls(intCount) = New clsComboBox Set objControls(intCount).ComboBox = objShape.OLEFormat.Object ElseIf TypeOf objShape.OLEFormat.Object Is OptionButton Then ' add event handlers for option buttons End If Next End Sub 

This code should go into the module of the clsComboBox class:

 Option Explicit Private WithEvents mobjComboBox As MSForms.ComboBox Friend Property Set ComboBox(objComboBox As MSForms.ComboBox) Set mobjComboBox = objComboBox End Property Private Sub mobjComboBox_Change() MsgBox "Selection changed." End Sub Private Sub mobjComboBox_Click() MsgBox "Clicked." End Sub 

Note that the objControls variable must be of type clsComboBox. Declaring this variable as an object or option did not work for me (can someone explain why ???).

+3
source

All Articles