Java, Jacob和Microsoft Word:如何正确处理事件



我的目标是编写一个java applet应用程序,该应用程序在客户端机器的临时目录中写入word文档(该文档是从DB中获取的),并使用Jacob打开该文档。

通过Jacob,我需要保留打开的文档的句柄,以便在用户关闭文档后,我需要将其保存回具有更改的DB。

也就是说,我想知道的第一件事是如何在用户关闭/退出MS Word文档时通过Jacob捕获关闭/退出事件。我怎样才能做到这一点呢?

我尝试了下面的代码,这是基于我在这个答案中看到的代码:https://stackoverflow.com/a/12332421/3813385但它只打开文档,不听关闭事件…

package demo;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.DispatchEvents;
import com.jacob.com.Variant;
public class WordEventTest { 
  public static void main(String[] args) {
      WordEventTest wordEventTest = new WordEventTest();
      wordEventTest.execute();
  }
  public void execute() {

      String strDir = "D:\fabricasw\workspace\jacob\WebContent\docs\";
      String strInputDoc = strDir + "file_in.doc";
      String pid = "Word.Application";
      ActiveXComponent axc = new ActiveXComponent(pid);
      axc.setProperty("Visible", new Variant(true));
      Dispatch oDocuments = axc.getProperty("Documents").toDispatch();
      Dispatch oDocument = Dispatch.call(oDocuments, "Open", strInputDoc).toDispatch();
      WordEventHandler w = new WordEventHandler();
      new DispatchEvents(oDocument, w);
  }
  public class WordEventHandler {
      public void Close(Variant[] arguments) {
          System.out.println("closed word document");
      }
  }
如果你们发布一些java代码来展示如何使用,我会很感激。至少如何获取Microsoft Word文档的内容以及如何检测应用程序关闭事件

对于处理事件,我从这个网站得到了帮助:http://danadler.com/jacob/

这是我的解决方案:

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.jacob.com.DispatchEvents;
import com.jacob.com.Variant;
import com.microsoft.word.WordApplication;
import com.microsoft.word.WordDocument;
import com.microsoft.word.WordDocuments;
public class WordEventDemo {
private WordApplication wordApp = null;
private WordDocuments wordDocs = null;
private WordDocument wordDoc = null;
private WordAppEventListener wordAppEventListener = null;
private WordDocEventListener wordDocEventListener = null;
private List<DispatchEvents> dispatchEvents = new ArrayList<DispatchEvents>();
public WordEventDemo() {
}
/**
 * Start Word, open the document and register listener to Word events
 * 
 * @param docId
 *            The id of the document in the database
 */
public void start(String filename) throws Exception {
    // get document from DB
    File fFile = new File(filename); // replace by your code to retrieve file from your DB
    // open document
    // create WORD instance
    wordApp = new WordApplication();
    // get document list
    wordDocs = wordApp.getDocuments();
    Object oFile = fFile.getAbsolutePath();
    Object oConversion = new Boolean(false);
    Object oReadOnly = new Boolean(false);
    wordDoc = wordDocs.Open(oFile, oConversion, oReadOnly);
    wordDoc.Activate();
    wordApp.setVisible(true);
    // register listeners for the word app and document
    wordAppEventListener = new WordAppEventListener();
    dispatchEvents.add(new DispatchEvents(wordApp, wordAppEventListener));
    wordDocEventListener = new WordDocEventListener();
    dispatchEvents.add(new DispatchEvents(wordDoc, wordDocEventListener));
}
// This is the event interface for the word application
public class WordAppEventListener {
    public WordAppEventListener() {
    }
    /**
     * Triggered when the Word Application is closed.
     */
    public void Quit(Variant[] args) {
        // Perform operations on "Quit" event
        System.out.println("quitting Word!");
    }
    /**
     * Event called by Word Application when it attempt to save a file.<br>
     * For Microsoft API reference, see <a
     * href="http://msdn.microsoft.com/en-us/library/ff838299%28v=office.14%29.aspx"
     * >http://msdn.microsoft.com/en-us/library/ff838299%28v=office.14%29.aspx</a>
     * 
     * @param args
     *            An array of 3 Variants (WARNING, they are not in the same order indicated in the msdn link)
     * @param args
     *            [0] <b>Cancel</b> : False when the event occurs. If the event procedure sets this argument to
     *            True, the document is not saved when the procedure is finished.
     * @param args
     *            [1] <b>SaveAsUI</b> : True to display the Save As dialog box.
     * @param args
     *            [2] <b>Doc</b> : The document that is being saved.
     */
    public void DocumentBeforeSave(Variant[] args) {
        // Perform operations on "DocumentBeforeSave" event
        System.out.println("saving Word Document");
    }
}
// This is the event interface for a word document
public class WordDocEventListener {
    /**
     * Triggered when a Word Document is closed.
     * 
     * @param args
     */
    public void Close(Variant[] args) {
        // Perform operations on "Close" event
        System.out.println("closing document");
    }
}
}

然后我像下面这样简单地调用它:

WordEventDemo fixture = new WordEventDemo();
fixture.start("path/to/file.docx");
// add a waiting mechanism (could be linked to the close or quit event), to make it simple here
Thread.sleep(20000);

相关内容

  • 没有找到相关文章