程序代码如下:
public void mhtToWord(String mhtFile, String wordFile) {
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
try {
app.setProperty("Visible", new Variant(false));
Dispatch wordDoc = app.getProperty("Documents").toDispatch();
wordDoc = Dispatch.invoke(wordDoc, "Add", Dispatch.Method,
new Object[0], new int[1]).toDispatch();
Dispatch.invoke(app.getProperty("Selection").toDispatch(),
"InsertFile", Dispatch.Method, new Object[] { mhtFile, "",
new Variant(false), new Variant(false),
new Variant(false) }, new int[3]);
Dispatch.invoke(wordDoc, "SaveAs", Dispatch.Method, new Object[] {
wordFile, new Variant(1) }, new int[1]);
Dispatch.call(wordDoc, "Close", new Variant(false));
} catch (Exception e){
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
mhtFile传进来的值为:"D:/20120227191436.html".
出现的异常为:
com.jacob.com.ComFailException: Invoke of: InsertFile
Source: Microsoft Word
Description: 文档名或其路径无效。 (D:/20120227191436.html)at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
at com.jacob.com.Dispatch.invoke(Dispatch.java:498)
at org.util.WordManager.mhtToWord(WordManager.java:103)
真的不太懂为什么这里的路径或者文件名无效... 求指教
public void mhtToWord(String mhtFile, String wordFile) {
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
try {
app.setProperty("Visible", new Variant(false));
Dispatch wordDoc = app.getProperty("Documents").toDispatch();
wordDoc = Dispatch.invoke(wordDoc, "Add", Dispatch.Method,
new Object[0], new int[1]).toDispatch();
Dispatch.invoke(app.getProperty("Selection").toDispatch(),
"InsertFile", Dispatch.Method, new Object[] { mhtFile, "",
new Variant(false), new Variant(false),
new Variant(false) }, new int[3]);
Dispatch.invoke(wordDoc, "SaveAs", Dispatch.Method, new Object[] {
wordFile, new Variant(1) }, new int[1]);
Dispatch.call(wordDoc, "Close", new Variant(false));
} catch (Exception e){
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
}
mhtFile传进来的值为:"D:/20120227191436.html".
出现的异常为:
com.jacob.com.ComFailException: Invoke of: InsertFile
Source: Microsoft Word
Description: 文档名或其路径无效。 (D:/20120227191436.html)at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
at com.jacob.com.Dispatch.invoke(Dispatch.java:498)
at org.util.WordManager.mhtToWord(WordManager.java:103)
真的不太懂为什么这里的路径或者文件名无效... 求指教
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货