java中通过jacob调用word方法

java中通过jacob调用word方法

public static void main(String[] args) {
    //启动word,生成一个ActivexComponent对象
    ActiveXComponent app = new ActiveXComponent("Word.Application");
    //要转换的word文件
    String inFile = "D:\\project\\java_word\\java_word\\wordtemplet.doc";
    //要报存的目标文件
    String tpFile ="D:\\project\\java_word\\java_word\\wordtemplet3.doc";
    boolean flag = false;
    Dispatch xlo = app.getObject();
    String oldText="1234";
    String newText="test";
    boolean visible=false;
    String bookMarkKey="LB_KJGG";
    try {
      //设置word不可见
      app.setProperty("Visible", new Variant(visible)); 字串7
      //log.info("设置word不可见成功!");
      System.out.println("设置word不可见成功!");
      Dispatch docs = app.getProperty("Documents").toDispatch();
      Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method
                                   ,new Object[]
                                   {inFile, new Variant(false), new Variant(true)}
                                   , new int[1]).toDispatch(); //打开word文件

字串8


      //在word2003的vba文档中application有UserName属性。
      String userName=app.getPropertyAsString("UserName");
      System.out.println("用户名:"+userName);
      Dispatch selection=app.getProperty("Selection").toDispatch();
      //得到一个组件
      System.out.println("Selection");
      Dispatch find = app.call(selection, "Find").toDispatch();
字串7


       //查找什么文本
       Dispatch.put(find, "Text", oldText);
       //替换文本
       Dispatch.call(find,"ClearFormatting");
       Dispatch.put(find, "Text", oldText);
       Dispatch.call(find, "Execute");
       Dispatch.put(selection, "Text", newText);
       // Dispatch.call(app, "SaveAs", inFile);
       System.out.println("replace");
      
       //把指定的值设置到指定的标签中去
       Dispatch activeDocument=app.getProperty("ActiveDocument").toDispatch();
       System.out.println("activedocument"); 字串1
       Dispatch bookMarks = app.call(activeDocument, "Bookmarks").toDispatch();
       System.out.println("bookmarks");
       boolean bookMarkExist1=Dispatch.call(bookMarks,"Exists",bookMarkKey).toBoolean();
       if(bookMarkExist1==true){
         System.out.println("exists bookmark!");
         Dispatch rangeItem = Dispatch.call(bookMarks, "Item",bookMarkKey).
                        toDispatch();
                    System.out.println("range item!");
                    Dispatch range = Dispatch.call(rangeItem, "Range").toDispatch(); 字串4
                    System.out.println("range !");
                    //取标签的值
                    String bookMarkValue=Dispatch.get(range,"Text").toString();
                    bookMarkValue="test";
                    if(bookMarkValue!=null){
                      Dispatch.put(range, "Text",
                             new Variant(bookMarkValue)); 字串4
                    }
        
       }
       else{
         System.out.println("not exists bookmark!");
       }
      //保存文件
      Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {tpFile, new Variant(0)}                      , new int[1]);
      //作为word格式保存到目标文件
      Variant f = new Variant(false);
      Dispatch.call(doc, "Close", f);
      flag = true;
    } 字串4
    catch (Exception e) {
      e.printStackTrace();
    }
    finally {
      app.invoke("Quit", new Variant[] {});
    }

字串9

你可能感兴趣的:(java,F#,VBA)