Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a VBA Macro by clicking Word 2010 Checkbox?

I want to run a macro when I click a checkbox in Word 2010.

Note that I neither want the "Legacy Forms" checkbox nor the "ActiveX" ones! They do only work in some "protected document mode" and look ugly, but I want the new ones which can be selected and unselected just when you write the document, and which look much nicer to me.

I know, with the legacy forms, you can directly insert a Macro when entering the form element and one for leaving it, and you can catch the event in VBA like

Sub CheckboxXY_Click()

But that does not work with the Word 2010 checkboxes, even when I give them a description and a tag name.

Repeat: these are the forms I want to use (just in case somebody would advise me to use the Legacy ones):

Word 2010 checkbox

And that's how they look like in the document (with mouse hover):

enter image description here

I cannot believe that I was the first one who tried this...

like image 414
Ingmar Avatar asked Jan 30 '14 12:01

Ingmar


People also ask

How do I run a macro from a check box?

Run the Add Macros Code To assign the CheckBoxDate macro to each check box, run the SetCheckBoxesMacro macro. You won't see a change on the worksheet, but now each check box has the macro assigned to it. To test the results, click on any one of the check boxes, and the Date cell in that row should change.

How do I run a macro in Word 2010?

To run a macro, click the button on the Quick Access Toolbar, press the keyboard shortcut, or you can run the macro from the Macros list. Click View > Macros > View Macros. In the list under Macro name, click the macro you want to run. Click Run.


1 Answers

Make sure you go to the Document in your VBA Project and select "ContentControlOnEnter"

You will have to specify which contentcontrol you want by using something like ContentControl.Title to specify which checkbox activates which part of your code as shown in the example below. (I also put in code to verify that the checkbox is checked in the example)

Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
    If (ContentControl.Title = "Checkbox1" And ContentControl.Checked = True) Then
        MsgBox "YAAY", vbOKOnly, "Test1"
    End If
    If (ContentControl.Title = "Checkbox2" And ContentControl.Checked = True) Then
        MsgBox "BOOO", vbOKOnly, "Test2!"
    End If
End Sub
like image 185
IAWeir Avatar answered Sep 20 '22 07:09

IAWeir