My goal is to write an java applet application that writes a word document(this document is fetched from DB)in a temporary directory on client machine and opens that document using Jacob.
Through Jacob I need to keep the handle to the opened document, so that after the user closes the document I need to save it back to the DB with the changes.
That said, the first thing I want to know is how to capture a close/exit event through Jacob when the user closes/exits the MS Word document. How can I achieve this?
I tried the code below, which is based in the code i saw in this answer: https://stackoverflow.com/a/12332421/3813385 but it only opens the document and does not listen the closing event...
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");
}
}
I would appreciate if you guys post some java code showing how. At least how to obtain the contents of a Microsoft Word document and how to detect the application closing event.