Share via

Microsoft 365 - Runtime error in macro for clear fields button

Anonymous
2020-12-03T04:07:45+00:00

Hi everyone - I am trying to get a functioning button to clear form fields using the macro below (Office 365). The data in the form fields will change but there is additional data in the document that remains the same year over year. It's working for about half of the form but a 6124 Runtime error is generated when it gets to the checkboxes. *"Run-time error '6124' You are note allowed to edit this selection because it is protected.*When I choose the debug option, the bolded line of code below is highlighted in yellow as the error. Any help would be greatly appreciated! PS - I am not a coder and using trial and error based on a string of code found on a forum.

Private Sub CommandButton1_Click()

Dim aCC As ContentControl

For Each aCC In ActiveDocument.ContentControls

aCC.Range.Select

If aCC.Type <> wdContentControlDropdownList Then

aCC.Range.Text = ""

Else

aCC.Type = wdContentControlText

aCC.Range.Text = ""

aCC.Type = wdContentControlDropdownList

aCC.Range.Text = ""

aCC.Type = wdContentControlCheckBox

aCC.Range.Text = "FALSE"

End If

Next aCC

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

  1. Anonymous
    2020-12-03T13:02:34+00:00

    I think the error message is misleading because you cannot clear a Content Control checkbox with setting Range.Text to a zero length. You also cannot clear DropDown Lists or Combo Boxes by attempting to clear a text range.

    Your code should look something like this:

    Private Sub CommandButton1_Click()

        Dim aCC As contentControl

        For Each aCC In ActiveDocument.ContentControls

            If aCC.Type = wdContentControlCheckBox Then

                aCC.Checked = False

            ElseIf aCC.Type = wdContentControlText Or aCC.Type = wdContentControlRichText Then

                aCC.Range.Text = ""

            ElseIf aCC.Type = wdContentControlDropdownList Or aCC.Type = wdContentControlComboBox Then

                aCC.DropdownListEntries(1).Select

            End If

        Next aCC

    End Sub

    Hope this helps

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments

6 additional answers

Sort by: Most helpful
  1. Anonymous
    2020-12-04T20:21:03+00:00

    Rich,

      When clearing ComboBoxes and DDLs, I like to convert type and set range to vbNullString then convert back.  I do this because in some case (e.g., these types of CCs created with code) there is no placeholder text entry (item 1) in the CC dropdown list.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2020-12-04T20:14:10+00:00

    You might try this:

    Private Sub CommandButton1_Click()

    Dim oCC As ContentControl

    Dim lngType As Long

      For Each oCC In ActiveDocument.ContentControls

        Select Case oCC.Type

          'Richtext, Plaintext, Date

          Case 0, 1, 6: oCC.Range.Text = vbNullString

          'Picture

          Case 2

            If oCC.Range.InlineShapes.Count = 1 Then oCC.Range.InlineShapes(1).Delete

          'Combobox, DropDownList

          Case 3, 4

            lngType = oCC.Type

            oCC.Type = 1

            oCC.Range.Text = vbNullString

            oCC.Type = lngType

          'Checkbox

          Case 8: oCC.Checked = False

          'Repeating section

          Case 9

            For lngType = oCC.RepeatingSectionItems.Count To 2 Step -1

              oCC.RepeatingSectionItems(lngType).Delete

            Next lngType

            If oCC.RepeatingSectionItems(1).Range.ContentControls.Count = 0 Then

              oCC.RepeatingSectionItems(1).Range.Text = vbNullString

            End If

        End Select

       Next oCC

    lbl_Exit:

      Exit Sub

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2020-12-04T16:27:14+00:00

    Hi F'ingChels

    I don't see that happening in my tests of the code here. In the case of DropDown lists all the code does is reset what is shown to the first item in the list, which is usually the default text.

    Please send me a copy of the document that I can test with here. The address to use is in my signature line.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2020-12-04T04:01:58+00:00

    Thank you for taking the time to respond, Rich! That does work to clear the fields but it not only clears the answers, but also wipes the fields of any set information. For example, it wipes the options available for selection in a drop-down menu in addition to the name selected and then leaves a blank field.

    Can that be avoided and clear only selections and not wipe the field formats/options?

    Was this answer helpful?

    0 comments No comments