如何将按钮添加到 Word 2007 文档,并使用 VBA 处理单击事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12588472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add button to word 2007 document, and handle click event with VBA?
提问by hendrix
How do I:
我如何能:
add button to my ms word 2007 document (i would say this would be trivial task, but can't even figure THIS out)
handle click event of this button in my VBA script
set text of my text control (with tag "text1")
将按钮添加到我的 ms word 2007 文档(我会说这将是微不足道的任务,但甚至无法弄清楚)
在我的 VBA 脚本中处理此按钮的单击事件
设置我的文本控件的文本(带有标签“text1”)
回答by Olle Sj?gren
I don't know if it is what you intended, but to add a button in the document, open the Developer tab. Click the Legacy tools
folder (in the Controls group) and click the Command Button (Active X Control)
. This will put a button in the document and make Word go into design mode.
我不知道这是否是您想要的,但要在文档中添加按钮,请打开“开发人员”选项卡。单击Legacy tools
文件夹(在控件组中),然后单击Command Button (Active X Control)
。这将在文档中放置一个按钮并使 Word 进入设计模式。
Double-click the new button and you will enter the VBA editor in the event handler for the button.
双击新按钮,您将在按钮的事件处理程序中进入 VBA 编辑器。
Enter code to manipulate the text in TextBox1:
输入代码来操作 TextBox1 中的文本:
TextBox1.Text = "Hi there, VBA master!"
Click the design mode button to exit design mode, and you're good to go; click the new button to set the text.
单击设计模式按钮退出设计模式,一切顺利;单击新按钮以设置文本。
If you meant to add a button in the ribbon? A little more complicated, but not that hard. Leave a comment, and I'm sure we can work it out as well.
如果您打算在功能区中添加一个按钮?稍微复杂一点,但没那么难。发表评论,我相信我们也可以解决这个问题。