在 Word VBA 中使用 Application.Run 传递参数

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

Passing Argument with Application.Run in Word VBA

vbaword-vba

提问by Tejas

I have the following two Sub defined in my Word Addin (.dotm) which I have put in StartUp directory

我在我的 Word Addin (.dotm) 中定义了以下两个 Sub,我已将其放入 StartUp 目录

Public Sub SayHi1()
    MsgBox "Hi......."
End Sub

Public Sub SayHi2(ByVal n As String)
    MsgBox "Hi " & n
End Sub

Then from a new document I am able to call 1st Sub without argument as below:

然后从一个新文档中,我可以不带参数地调用 1st Sub,如下所示:

Sub AppRun_AddIn_NoArg()
    Application.Run "MyProject.Module1.SayHi1"
End Sub

But when I try to run the 2nd Sub with argument I get error saying "Object doesn't support this property or method"

但是当我尝试使用参数运行第二个 Sub 时,我收到错误消息“对象不支持此属性或方法”

Sub AppRun_AddIn_WithArg()
    Application.Run "MyProject.Module1.SayHi2", "Tejas"
End Sub

Error Message: enter image description here

错误信息: 在此处输入图片说明

采纳答案by GSerg

This appears to be long-standing problem with Word.

这似乎是 Word 的长期问题。

As KB190235 suggests:

正如KB190235 所建议的

Cause:
You have included a template name as part of the Macroname argument string.

Resolution:
Remove the template name from the Macroname argument.

Workaround:
To avoid naming conflicts among referenced projects, give your procedures unique names, so that you can call a procedure without specifying a project or module.

原因:
您已将模板名称作为 Macroname 参数字符串的一部分包含在内。

解决方法:
从 Macroname 参数中删除模板名称。

解决方法:
为避免引用项目之间的命名冲突,请为您的过程指定唯一名称,以便您可以在不指定项目或模块的情况下调用过程。