日期:2014-05-19  浏览次数:20973 次

jacob操作word,插入图片

才开始研究jacob,求指点

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class TestP {
  public static void main(String[] args) {
ActiveXComponent app = new ActiveXComponent("Word.Application"); 
try {
Dispatch.put(app, "Visible", new Variant(true));
Dispatch documents = Dispatch.get(app, "Documents").toDispatch();
Dispatch document = Dispatch.call(documents, "Add").toDispatch();
Dispatch selection = app.getProperty("Selection").toDispatch();
Dispatch picture = Dispatch.call(selection, "InlineShapes").toDispatch();
Dispatch.invoke(picture,"AddPicture",Dispatch.Method,new Object[] { "E://lw.jpg",new Variant(false), new Variant(true) }, new int[1]).toDispatch();
Dispatch.call(document, "SaveAs", "E://myFile.doc"); 
Dispatch.call(document, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
app.safeRelease();
}
  }
}

异常信息:
com.jacob.com.ComFailException: Invoke of: AddPicture
Source: Microsoft Word
Description: 
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:804)
at com.jacob.com.Dispatch.invoke(Dispatch.java:483)
at TestP.main(TestP.java:15)

------解决方案--------------------
http://www.68design.net/Development/JSP/41894-1.html
http://sinye.iteye.com/blog/588050
http://blog.donews.com/dukejoe/archive/2010/04/28/1575574.aspx

推荐链接