Can't call Word VBA Macro with parameters

£可爱£侵袭症+ 提交于 2019-12-11 02:49:08

问题


I have a VB6 application that needs to call a Word 2010 VBA routine and supply a string parameter. The VBA routine is in an Addin that is enabled with the open document.

According to the MSDN reference (http://msdn.microsoft.com/en-us/library/ff838935.aspx) I should be able to supply that parameter after the macro name, in order.

My code calling the routine looks like so:

sMacro = "Link.Functions.UpdateFootnote"
sParam = "Footnote Text"
DocApp.Run sMacro, sParam

'I've also tried
DocApp.Run MacroName:=sMacro, varg1:=sParam
'and
DocApp.Run "Link.Functions.UpdateFootnote","Footnote Text"

In each case this yields run-time error 438, "Object doesn't support this property or method."

DocApp.Run "Link.Functions.UpdateFootnote, Footnote text"

This one gives Run-time error -2147352573(80020003) "Unable to run the specified macro"

As a check, I have a 2nd parameterless macro (that then calls the original macro) and it works fine.

DocApp.Run "Link.Functions.UpdateFootnoteTest"

What am I getting wrong here?


回答1:


Word is fussy about the ActiveDocument when it comes to using Application.Run. If the function/procedure is in another document (other than the main document's template) that is referenced by the first document, it is sometimes necessary to activate the referenced document first.

Even with the document activated, it is not possible to qualify the function name with the project name. The best you can do is use the module name

Link.ThisDocument.Activate
Application.Run "Functions.Foo", "FOO"


来源:https://stackoverflow.com/questions/8015688/cant-call-word-vba-macro-with-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!