问题
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