Log in

View Full Version : VBA code to clear all option buttons



g8r777
07-27-2011, 11:54 AM
I have put a group of option buttons on a form that I am creating in Word. The option buttons are inside a table cell (as are all other form elements i.e. text content controls and drop down list content controls).

I have created the following macro (with the help of many in this forum) that resets and clears all form fields I have so far.

Sub ClearFields()
Dim oCC As ContentControl
Dim j As Long
Dim oControls As ContentControls
Dim oLE As ContentControlListEntry
Set oControls = ActiveDocument.ContentControls

For Each oCC In ActiveDocument.Range.ContentControls
If oCC.Type = wdContentControlRichText Then
oCC.Range.Text = " "
End If
Next

For j = 1 To oControls.Count
If oControls(j).Type = wdContentControlDropdownList Then
Set oLE = oControls(j).DropdownListEntries(1)
oLE.Select
End If
Next

End Sub

What I can't figure out is how to code for the clearing of the legacy option buttons. I want to be able to set the form to an entirely clear state with my macro meaning that none of the three option buttons are selected initially.

I appreciate any help.

Thank you,

Brian

gmaxey
07-27-2011, 02:47 PM
ActiveX optionbutton controls are an entirely different breed:

Sub ResetRadioButtons()
Dim oILS As InlineShape
Dim oCntr As Object
For Each oILS In ActiveDocument.InlineShapes
If oILS.Type = wdInlineShapeOLEControlObject Then
If TypeOf oILS.OLEFormat.Object Is MSForms.OptionButton Then
oILS.OLEFormat.Object.Value = False
End If
End If
Next
End Sub

g8r777
07-27-2011, 03:11 PM
I tried this and get a compile error: user-defined type not defined that highlights the following code:
TypeOf oILS.OLEFormat.Object Is MSForms.OptionButton

gmaxey
07-27-2011, 03:16 PM
In your VB Editor Toos>References. Set a reference to Microsoft Forms 2.0 Object Library.