下面是我从网上搜到的代码,并使用最新的jacob1.9版本,编译出错。源码如下:
import java.io.*;
import com.jacob.com.*;
import com.jacob.activeX.*;public class WordToHtml {
public static void change( String orginfile,String savefile ) {
File f = new File( orginfile ); ActiveXComponent wordApp = new ActiveXComponent( "Word.Application" );
try {
wordApp.setProperty( "Visible",new Variant(false) ); Object docs = wordApp.getProperty("Documents").toDispatch(); //打开word文件
Object file = Dispatch.invoke( docs,"Open",Dispatch.Method,new Object[]{f,new Variant(false),new Variant(true)},new int[1] ).toDispatch(); //作为html格式保存到临时文件
Dispatch.invoke( file,"Saveas",Dispatch.Method,new Object[]{savefile,new Variant(8)},new int[1] ); Dispatch.call( file,"Close",new Variant(false) );
}
catch( Exception e ) {
e.printStackTrace();
}
finally {
wordApp.invoke( "Quit",new Variant[]{} );
} System.out.println( "转换完毕。" );
} public static void main( String args[] ) {
change( "E:/a.doc","E:/temp.html" );
}
}编译出错信息:
WordToHtml.java:16: 找不到符号
符号: 方法 invoke(java.lang.Object,java.lang.String,int,java.lang.Object[],int[
])
位置: 类 com.jacob.com.Dispatch
                        Object file = Dispatch.invoke( docs,"Open",Dispatch.Meth
od,new Object[]{f,new Variant(false),new Variant(true)},new int[1] ).toDispatch(
);
                                              ^
WordToHtml.java:19: 找不到符号
符号: 方法 invoke(java.lang.Object,java.lang.String,int,java.lang.Object[],int[
])
位置: 类 com.jacob.com.Dispatch
                        Dispatch.invoke( file,"Saveas",Dispatch.Method,new Objec
t[]{savefile,new Variant(8)},new int[1] );
                                ^
WordToHtml.java:21: 找不到符号
符号: 方法 call(java.lang.Object,java.lang.String,com.jacob.com.Variant)
位置: 类 com.jacob.com.Dispatch
                        Dispatch.call( file,"Close",new Variant(false) );
                                ^
3 错误
各位使用过或精通jacob的帮忙解决一下,非常感谢!

解决方案 »

  1.   

    改用jacob1.7版本,执行出现异常:
    com.jacob.com.ComFailException: A COM exception has been encountered:
    At Invoke of: Documents
    Description: An unknown COM error has occured.
            at com.jacob.com.Dispatch.invokev(Native Method)
            at com.jacob.activeX.ActiveXComponent.getProperty(ActiveXComponent.java)        at WordToHtml.change(WordToHtml.java:13)
            at WordToHtml.main(WordToHtml.java:34)
    Exception in thread "main" com.jacob.com.ComFailException: A COM exception has b
    een encountered:
    At Invoke of: Quit
    Description: An unknown COM error has occured.
            at com.jacob.com.Dispatch.invokev(Native Method)
            at com.jacob.activeX.ActiveXComponent.invoke(ActiveXComponent.java)
            at WordToHtml.change(WordToHtml.java:27)
            at WordToHtml.main(WordToHtml.java:34)这不知什么情况,还有其他更好的方法吗?
      

  2.   

    在编译的时候指定classpath试试