Share via

Adding a MSForms.Checkbox at Cursor

Anonymous
2016-12-21T23:59:24+00:00

Hi all,

I want to use VBA to add an MSForms Checkbox at the cursor location in the ActiveDocument.

When I try this code, it throws a "Run-time error 4218 - Type mismatch"

Sub AddCheckbox()

 Dim cbxAdded As MSForms.CheckBox

 Set cbxAdded = ActiveDocument.Shapes _

    .AddOLEControl(ClassType:="MSForms.CheckBox", _

         Anchor:=Selection)

End Sub

The VB Project has a reference to Microsoft Forms 2.0 Object Library.

I've been able to use similar code to add Forms.CheckBox.1, but that's a different control than the ones in my existing document.

Thanks, -Jerry

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

Doug Robbins - MVP - Office Apps and Services 323.1K Reputation points MVP Volunteer Moderator
2016-12-22T23:37:15+00:00

To have the checkbox inserted in-line with text (and hence without an anchor), use

 Dim cbxAdded As Object

 Set cbxAdded = ActiveDocument.InlineShapes _

    .AddOLEControl(ClassType:="Forms.CheckBox.1", Range:=Selection.Range)

Was this answer helpful?

0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Anonymous
    2016-12-22T18:02:15+00:00

    Greg, Thanks for your response. Yes, the existing checkboxes are ActiveX controls: Forms.Checkbox.1.

    I'm able to add those using this code:

    Sub AddCheckbox()

     Dim cbxAdded As Object

     Set cbxAdded = ActiveDocument.Shapes _

        .AddOLEControl(ClassType:="Forms.CheckBox.1", _

        Anchor:=Selection.Range)

    End Sub

    After matching all the properties of my existing checkboxes, these added checkboxes look identical except for one difference. They have an anchor symbol that appears when hovering over the control. That might not matter, but I'd like them to match the existing settings, if possible.

    When I manually add an ActiveX Checkbox from the "Legacy" controls in the toolbox, this anchor doesn't appear.

    Comparing the dialogs for Right-click > Format AutoShape/ Picture, these added Checkboxes have 3 tabs greyed out (Colors and Lines, Picture, Text Box). The existing Checkboxes and ones that I add manually don't have any tabs greyed out.

    Any suggestions on how to match the existing and manually-added checkboxes would be appreciated.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-12-22T15:22:03+00:00

    I don't think MSForms.Checkbox is in you existing document because those are found in userforms.  I you existing document you either have an ActiveX control Forms.Checkbox.1, a formfield checkbox, or a content control checkbox.

    Sub ScratchMacro()

    'A basic Word macro coded by Greg Maxey

       Selection.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormCheckBox

       'or

       Selection.Range.ContentControls.Add wdContentControlCheckBox

    lbl_Exit:

      Exit Sub

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2016-12-22T15:13:44+00:00

    Then what is the Office - Programming Answer forum supposed to be for?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2016-12-22T14:36:49+00:00

    Hi Jerry,

    For assistance regarding VBA coding and Office programming concerns, we recommend that you post your query on the MSDN forums.

    Kind regards.

    Was this answer helpful?

    0 comments No comments