Word 模板:单击图形按钮执行 VBA 宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18174819/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:42:25  来源:igfitidea点击:

Word Template : Executing VBA macro on a graphical button click

vbams-officeword-vba

提问by MankitaP

I've created a VBA Macro for word template and I want to associate it to a button(Graphics button, Not keyboard buttons).There are basically two ways to create a macro :

我已经为 word 模板创建了一个 VBA 宏,我想将它关联到一个按钮(图形按钮,不是键盘按钮)。基本上有两种方法可以创建一个宏:

1) Using macro recorder

1) 使用宏记录器

2) Using VBA for application to write macro from scratch

2)使用VBA为应用程序从头开始编写宏

But I found several issues with these ways :

但是我发现这些方式有几个问题:

I tried First one, What I want is to execute a macro on a button onclick event. but in that I am not getting how to assign that macro to that button.

我尝试了第一个,我想要的是在按钮 onclick 事件上执行宏。但我不知道如何将该宏分配给该按钮。

For second option I tried following procedure :

对于第二个选项,我尝试了以下程序:

Goto VB by pressing Alt + F11. On the Tools menu, click References. Select the reference for Microsoft Visual Basic for Applications Extensibility. Insert a new module, and then add the following code example.

按 Alt + F11 转到 VB。在工具菜单上,单击引用。选择 Microsoft Visual Basic for Applications Extensibility 的参考。插入一个新模块,然后添加以下代码示例。

 Sub Test()

'Add a command button to a new document
Dim doc As Word.Document
Dim shp As Word.InlineShape
Set doc = Documents.Add

Set shp = doc.Content.InlineShapes.AddOLEControl(ClassType:="Forms.CommandButton.1")
shp.OLEFormat.Object.Caption = "Click Here"

'Add a procedure for the click event of the inlineshape
'**Note: The click event resides in the This Document module
Dim sCode As String
sCode = "Private Sub " & shp.OLEFormat.Object.Name & "_Click()" & vbCrLf & _
        "   MsgBox ""You Clicked the CommandButton""" & vbCrLf & _
        "End Sub"
doc.VBProject.VBComponents("ThisDocument").CodeModule.AddFromString sCode

End Sub

This is the code for macro creation through Vb.But here error is coming in secondlast line of code.

这是通过 Vb 创建宏的代码。但是这里的错误出现在第二行代码中。

Can anyone suggest me how to get an executable macro on a button click? Am I doing any mistakes in above code?

谁能建议我如何通过单击按钮获取可执行宏?我在上面的代码中有什么错误吗?

回答by n00b

For office 2010 that I have:

对于 Office 2010,我有:

1.Macros can be assigned to buttons that will show up in top-left corner of your window (Quick Access Toolbar) When you create a macro, it asks if you want to assign it to keyboard or Button where you can select button

1.宏可以分配给将显示在窗口左上角的按钮(快速访问工具栏)当您创建宏时,它会询问您是否要将其分配给键盘或按钮,您可以在其中选择按钮

2.You can add a command buttonor any other control that will run a code when you click it. To do so first you have to add 'Command Button' to your ribbon so that you can create button in your page. To add it you can click on Office button, go to Options, then go to Customize Ribbonand then select All commandsfrom Choose commands fromdrop down list. find the Command Buttonand add it somewhere to you ribbon. You may have to create a custom tab in your ribbon. When you have the button in your ribbon, then you can add button to your page and after adding the ribbon double-click on it, and you can write code in button's event handler. You can copy & paste your Macro code there or call the macro using callcommand How to add a button and call a Macro

2.您可以添加一个command button或任何其他控件,当您单击它时将运行代码。为此,您首先必须将“命令按钮”添加到功能区,以便您可以在页面中创建按钮。要添加它,您可以单击 Office 按钮,转到Options,然后转到Customize Ribbon然后All commandsChoose commands from下拉列表中选择。找到Command Button并将其添加到功能区的某个位置。您可能需要在功能区中创建自定义选项卡。当您的功能区中有按钮时,您可以向页面添加按钮,添加功能区后双击它,您可以在按钮的事件处理程序中编写代码。您可以在那里复制并粘贴您的宏代码或使用call命令 调用宏如何添加按钮并调用宏

回答by Harif87

In regards to your issue with the command button showing the VB code when clicking instead of actually running - make sure you are not in Design Mode. Look under the Developer tab on your ribbon and make sure Design Mode is not highlighted.

关于在单击而不是实际运行时显示 VB 代码的命令按钮的问题 - 确保您不在设计模式下。查看功能区上的“开发人员”选项卡下,确保未突出显示设计模式。