如何通过单击Word 2010复选框运行VBA宏? [英] How to run a VBA Macro by clicking Word 2010 Checkbox?

查看:196
本文介绍了如何通过单击Word 2010复选框运行VBA宏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击 Word 2010 中的复选框时,我想运行宏.

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

请注意,我既不需要旧版表单"复选框,也不需要"ActiveX"复选框.它们只在某些受保护的文档模式"下工作且外观丑陋,但是我想要新的可以在编写文档时选择和取消选择,这对我来说看起来更好.

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.

我知道,使用旧版表单,您可以在输入表单元素时直接插入一个宏,然后将其留出,然后可以像这样在VBA中捕获事件

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()

但这不适用于Word 2010复选框,即使我给它们提供了描述和标记名称也是如此.

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):

这就是它们在文档中的外观(使用鼠标悬停):

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

我不敢相信我是第一个尝试过此方法的人...

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

推荐答案

确保您转到VBA项目中的文档"并选择"ContentControlOnEnter"

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

您将必须使用诸如ContentControl.Title之类的东西来指定所需的contentcontrol,以指定哪个复选框激活代码的哪一部分,如下例所示. (我还输入了代码来验证示例中的复选框是否选中)

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

这篇关于如何通过单击Word 2010复选框运行VBA宏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆