从 VBScript 调用外部 VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6192618/
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
Calling an External VBA from VBScript
提问by DurkD
I am using a program called mathtype to pull some equation objects out of a word document. I've written code in VBA that works perfectly using their API, but I have to translate it to a VBScript file. I have looked all over google, but have not found any solution on how (If it is even possible) to call a VBA library from VBScript.
我正在使用一个名为 mathtype 的程序从 Word 文档中提取一些方程对象。我已经在 VBA 中编写了代码,使用他们的 API 可以完美地工作,但我必须将其转换为 VBScript 文件。我已经在谷歌上找遍了,但没有找到任何关于如何(如果可能的话)从 VBScript 调用 VBA 库的解决方案。
VBScript can't see the MathTypeSDK Objects/Functions.
VBScript 看不到 MathTypeSDK 对象/函数。
If not possible, how would I encase the macro I need to run in a globally available word file and call it from the VBScript?
如果不可能,我如何将需要在全局可用的 word 文件中运行的宏封装起来并从 VBScript 中调用它?
Edit: Got it! Unfortunately the approaches below, while helpful, did not work for my situation. I found something closer: Embedding the macro in a global file and calling it through the Word Objects Run command. objWord.Run "Normal.NewMacros.RunMain"
编辑:明白了!不幸的是,下面的方法虽然有用,但对我的情况不起作用。我发现了一些更接近的东西:将宏嵌入到全局文件中并通过 Word Objects Run 命令调用它。objWord.Run "Normal.NewMacros.RunMain"
回答by Tim Williams
Here is an approach which might work for you. I tested this simple example.
这是一种可能对您有用的方法。我测试了这个简单的例子。
Class "clsTest" in file "Tester.docm":
文件“Tester.docm”中的“clsTest”类:
Public Sub Hello()
MsgBox "Hello"
End Sub
Class "Instancing" is marked "PublicNotCreatable".
“实例化”类被标记为“PublicNotCreatable”。
Module in "Tester.docm":
“Tester.docm”中的模块:
Public Function GetClass() As clsTest
Set GetClass = New clsTest
End Function
In your vbscript:
在你的 vbscript 中:
Dim fPath, fName
fPath = "C:\Documents and Settings\twilliams\Desktop\"
fName = "Tester.docm"
Dim wdApp, o
Set wdApp = CreateObject("word.application")
wdApp.visible=true
wdapp.documents.open fPath & fName
Set o = wdApp.Run("GetClass")
o.Hello
Set o=nothing
Again - I only tested this simple example: you'll have to adapt it to your situation and try it out.
再次 - 我只测试了这个简单的例子:你必须根据你的情况调整它并尝试它。
回答by Doc Brown
Word-VBA was not made to create reusable libraries, I suppose (for usage in external programs).
我想 Word-VBA 不是用来创建可重用的库的(用于外部程序)。
One way to reuse existing Word-VBA code is, however, run Word via WScript.Shell.Run
using the /m<macroname>
command line switch (see http://support.microsoft.com/kb/210565/en-usfor details). This, has the restriction that evertime you need to call a specific macro, a Word process is started again, running that macro, and ends afterwards. Means, if you need just one call to your Word.VBA for a specfific task, this may be ok, but if you need a lot of interprocess communication between your VBScript and your VBA macro, you should look for a different solution.
但是,重用现有 Word-VBA 代码的一种方法是通过WScript.Shell.Run
使用/m<macroname>
命令行开关运行 Word (有关详细信息,请参阅http://support.microsoft.com/kb/210565/en-us)。这有一个限制,即每次您需要调用特定宏时,都会再次启动 Word 进程,运行该宏,然后结束。意思是,如果您只需要一次调用 Word.VBA 来完成特定任务,这可能没问题,但是如果您需要在 VBScript 和 VBA 宏之间进行大量进程间通信,则应该寻找不同的解决方案。