vba 从java代码调用VB宏

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

Call a VB macro from java code

javavbaword-vba

提问by Holograham

I have a VB macro created. I want to pass the macro a string and a file location. How would I call this in java code. Is there a library for this?

我创建了一个 VB 宏。我想向宏传递一个字符串和一个文件位置。我将如何在 Java 代码中调用它。有图书馆吗?

采纳答案by mdma

You can run vbscript using the "cscript.exe" that ships with windows.

您可以使用 Windows 附带的“cscript.exe”运行 vbscript。

Depending upon your scenario, you can launch this from Java in a variety of ways:

根据您的场景,您可以通过多种方式从 Java 启动它:

  • use Runtime.execto launch the program. You can do this directly as part of your program.
  • use Ant, which has an exec task, or maven which has an exec plugin. This is most useful when invoking the script as part of a build or some other batch process.
  • 使用Runtime.exec启动程序。您可以将其作为程序的一部分直接执行。
  • 使用具有 exec 任务的 Ant 或具有 exec 插件的 maven。这在作为构建或其他批处理过程的一部分调用脚本时最有用。

EDIT: If your script has a GUI, then use "wscript.exe".

编辑:如果您的脚本有 GUI,则使用“wscript.exe”。

I'm assuming you mean vbscript, but if you reall mean a macro, such as a Word macro, then you will need to do something like this:

我假设您指的是 vbscript,但如果您真的指的是宏,例如 Word 宏,那么您需要执行以下操作:

"C:\Program Files\Microsoft Office\Office12\Winword.exe" 
"C:\MyPath\MyDoc.doc" /m"Macro1"

Alternatively, you can create a small vbscript that instantiates the Word Application and uses the run() method to invoke a macro. You execute that script using cscript.exe/wscript.exe.

或者,您可以创建一个小的 vbscript 来实例化 Word 应用程序并使用 run() 方法来调用宏。您可以使用 cscript.exe/wscript.exe 执行该脚本。

回答by Tomalak

There is the "JACOB - Java COM Bridge" on SourceForge. The project has a second, more dated homepage.

SourceForge 上“JACOB - Java COM Bridge”。该项目有第二个,更过时的主页

And then there is a commercial (D)COM/ActiveX automation library called J-Integra, which also looks like it could do such a thing.

然后有一个名为J-Integra的商业 (D)COM/ActiveX 自动化库,它看起来也可以做这样的事情。

Disclaimer: Those are just links I've pulled out of Google, I have no practical experience with these libraries.

免责声明:这些只是我从 Google 中提取的链接,我对这些库没有实际经验。

回答by Daniel

We are using Com4J, which created us a set of Java Classes which looks identical to the ActiveX classes usable from Word VBA, and call those directly. This obviously only works on Windows machines, since Word does not run on Linux (except for when you have much time and Wine).

我们正在使用 Com4J,它为我们创建了一组 Java 类,它们看起来与 Word VBA 中可用的 ActiveX 类相同,并直接调用它们。这显然只适用于 Windows 机器,因为 Word 不能在 Linux 上运行(除非你有很多时间和 Wine)。

By having these APIs ready in Java, we would be able to call any macro defined in word.

通过在 Java 中准备好这些 API,我们将能够调用 word 中定义的任何宏。