从java代码调用VB宏 [英] Call a VB macro from java code
问题描述
我创建了一个VB宏。我想传递宏的字符串和文件位置。我如何在java代码中调用它。是否有一个库?
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?
推荐答案
您可以使用Windows附带的cscript.exe运行vbscript。
You can run vbscript using the "cscript.exe" that ships with windows.
根据您的情况,您可以通过多种方式从Java启动它:
Depending upon your scenario, you can launch this from Java in a variety of ways:
- 使用 Runtime.exec 来启动程序。您可以直接在程序中执行此操作。
- 使用具有exec任务的Ant或具有exec插件的maven。这在作为构建或其他批处理过程的一部分调用脚本时非常有用。
- use Runtime.exec to 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.
编辑:如果脚本有GUI ,然后使用wscript.exe。
If your script has a GUI, then use "wscript.exe".
我假设你的意思是vbscript,但如果你的意思是一个宏,比如一个Word宏,那么你需要做这样的事情:
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:
"C:\Program Files\Microsoft Office\Office12\Winword.exe"
"C:\MyPath\MyDoc.doc" /m"Macro1"
或者,您可以创建一个小的vbscript来实例化Word应用程序并使用run()方法来调用宏。您可以使用cscript.exe / wscript.exe执行该脚本。
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.
这篇关于从java代码调用VB宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!