1. 下载jacob的包,将dll放到E:\Program Files\Java\jdk1.8.0_202\jre\bin目录下。
2. 引入依赖
<dependency>
<groupId>io.github.osobolev</groupId>
<artifactId>jacob</artifactId>
<version>1.20</version>
</dependency>
3. 使用
//开始处理
ComThread.InitSTA();
ActiveXComponent word = new ActiveXComponent("Word.Application");
//设置应用操作是文档不在明面上显示,只在后台静默处理。
word.setProperty("Visible", new Variant(false));
Dispatch documents = word.getProperty("Documents").toDispatch();
//指定要打开的文档并且打开它
if(path.indexOf("/") == 0){
path=path.substring(1);
}
Dispatch dis = Dispatch.call(documents, "Open", path).toDispatch();
//在这个文档上运行宏
Dispatch.call(word, "Run", new Variant("mathTypeToLatex"));
//如果要调用多个宏,要分开执行,不可以直接写在后面,那样会被当成参数,而不是一个宏。
word.invoke("Quit", new Variant[]{});
ComThread.Release();