Consulting

Results 1 to 4 of 4

Thread: VBA code to clear all option buttons

  1. #1
    VBAX Regular
    Joined
    Dec 2006
    Posts
    71
    Location

    VBA code to clear all option buttons

    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.

    [VBA]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[/VBA]

    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

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Dec 2006
    Posts
    71
    Location
    I tried this and get a compile error: user-defined type not defined that highlights the following code:
    [VBA]TypeOf oILS.OLEFormat.Object Is MSForms.OptionButton[/VBA]

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    In your VB Editor Toos>References. Set a reference to Microsoft Forms 2.0 Object Library.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •