/**
* Initializes a JACOB object to start the office process to
* manipulate the file.
* <p>
* 初始化一个JACOB对象启动office进程用来操作文件。
* </p>
*/
public JacobUtils() {
// 线程启动,COM组件初始化
ComThread.InitMTA(true);
if (this.word == null) {
long start = System.currentTimeMillis();
// 启动word
this.word = new ActiveXComponent("Word.Application");
long end = System.currentTimeMillis();
System.out.println("启动word总耗时:==========================" + (end - start));
// true-可见打开word;false-不可见打开word
this.word.setProperty("Visible", new Variant(false));
// 禁用宏
this.word.setProperty("AutomationSecurity", new Variant(3));
}
if (this.documents == null) {
// 初始化word的所有文档窗口
this.documents = this.word.getProperty("Documents").toDispatch();
}
}
通过java -jar ***.jar命令启动jar报后jacob运行一切正常。但将jar包通过nssm工具注册到windows服务后,操作jacob则报如下异常。
com.jacob.com.ComFailException: Can't get object clsid from progid
at com.jacob.com.Dispatch.createInstanceNative(Native Method) ~[jacob-1.19.jar!/:na]
at com.jacob.com.Dispatch.<init>(Dispatch.java:99) ~[jacob-1.19.jar!/:na]
at com.jacob.activeX.ActiveXComponent.<init>(ActiveXComponent.java:58) ~[jacob-1.19.jar!/:na]
at com.shiwen.zjbpm.common.util.JacobUtils.<init>(JacobUtils.java:98) ~[zjbpm-commons-0.0.1-SNAPSHOT.jar!/:0.0.1-SNAPSHOT]