Call a VB macro from java code

时光毁灭记忆、已成空白 提交于 2019-12-18 17:31:08

问题


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?


回答1:


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

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

  • 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.

EDIT: If your script has a GUI, then use "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:

"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.




回答2:


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

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

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




回答3:


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).

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



来源:https://stackoverflow.com/questions/2805763/call-a-vb-macro-from-java-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!