通过自定义按钮运行宏时出现 VBA 错误“参数数量错误或属性分配无效”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15409457/
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
VBA error 'Wrong number of arguments or invalid property assignments' when running macro via custom button
提问by David Gard
I have a macro that I call via a tab/group/button added by the Custom UI Editor -
我有一个通过自定义 UI 编辑器添加的选项卡/组/按钮调用的宏 -
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
<ribbon startFromScratch="false">
<tabs>
<tab id="tabCustomActions" label="Custom ActionsXXX" insertAfterMso="TabDeveloper">
<group id="GroupTLA" label="TLA Actions">
<button id="buttonFormatTLA" label="Format as TLA" image="TLALogo" size="large" onAction="start_tla" />
</group>
</tab>
</tabs>
</ribbon>
</customUI>
The button shows up just fine, with my custom logo, but when ever I click the button I get the follow error -
该按钮显示得很好,带有我的自定义徽标,但是每当我单击该按钮时,都会出现以下错误 -
VBA is not opened following this error, as it usually is, and no code within VBA is indicated as the problem if I open the developer console and then try and click the button.
出现此错误后,VBA 不会像往常一样打开,如果我打开开发人员控制台然后尝试单击按钮,则 VBA 中没有任何代码被指示为问题。
Strangely though, if I try and run the macro manually, it works fine with no errors. Does anybody have any ideas how to solve this?
奇怪的是,如果我尝试手动运行宏,它可以正常工作,没有错误。有没有人有任何想法如何解决这个问题?
Here is my full code in a Pastebin, should you wish to view it. Thanks.
如果您想查看它,这是我在Pastebin 中的完整代码。谢谢。
回答by Olle Sj?gren
You have the wrong call signature for the start_tla
callback in your VBA code.
您start_tla
的 VBA 代码中有错误的回调调用签名。
If you open up your file in Custom UI Editor, there is a button called Generate Callbacksto the right in the menu. If you press it, it will give you the correct callbacks for your VBA code to match the ribbon xml in your file:
如果您在Custom UI Editor 中打开您的文件,则菜单右侧有一个名为Generate Callbacks的按钮。如果您按下它,它将为您的 VBA 代码提供正确的回调,以匹配您文件中的功能区 xml:
'Callback for buttonFormatTLA onAction
Sub start_tla(control As IRibbonControl)
'Your code goes here
End Sub
According to the Pastebin link, your sub looks like this, without the control As IRibbonControl
parameter:
根据 Pastebin 链接,您的 sub 看起来像这样,没有control As IRibbonControl
参数:
Public Sub start_tla()