I am writing a form in Word 2003 to collect multiple responses to a single question. I have a macro on a button press which duplicates the various input fields (drop-down boxes, radio buttons etc.) ready for a new response.
However, I need to change the text of the radio buttons, and set the OnChange
event on a combobox, and I can't find the correct syntax to do so. Both the controls are from the 'Control Toolbox' toolbar.
The macro code I have to duplicate the controls is below.
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
adding the event handlers dynamically is a bit tricky.
You could add the code dynamically to ThisDocument. This is the way 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 for event handling and create an instance of this class for each control. Place the following code into a 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 a class module named clsComboBox:
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 variable objControls must be of type clsComboBox. Declaring this variable as Object or Variant didn't work for me (could anyone explain why???).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.