无法使用参数调用Word VBA宏 [英] Can't call Word VBA Macro with parameters

查看:114
本文介绍了无法使用参数调用Word VBA宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VB6应用程序,需要调用Word 2010 VBA例程并提供字符串参数. VBA例程位于随打开的文档启用的Addin中.

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.

根据MSDN参考(http://msdn.microsoft.com/zh-cn/library/ff838935.aspx),我应该能够按顺序在宏名称后提供该参数.

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"

在每种情况下,都会产生运行时错误438,对象不支持此属性或方法."

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

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

此错误给出了运行时错误-2147352573(80020003)无法运行指定的宏"

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"

我在这里怎么了?

推荐答案

在使用Application.Run时,对ActiveDocument的说法很挑剔.如果功能/过程位于第一个文档所引用的另一个文档中(而不是主文档的模板),则有时必须首先激活该引用的文档.

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"

这篇关于无法使用参数调用Word VBA宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆