【问题标题】:Java, Jacob and Microsoft Word: how to properly handle events?Java、Jacob 和 Microsoft Word:如何正确处理事件?
【发布时间】:2014-10-21 17:39:05
【问题描述】:

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

通过 Jacob,我需要保留打开文档的句柄,以便在用户关闭文档后,我需要将其与更改一起保存回数据库。

也就是说,我想知道的第一件事是当用户关闭/退出 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文档的内容以及如何检测应用程序关闭事件

【问题讨论】:

    标签: java event-handling ms-word jacob


    【解决方案1】:

    为了处理事件,我从这个站点获得了帮助: 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);
    

    【讨论】:

      猜你喜欢
      • 2011-03-04
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 1970-01-01
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多