gpt4 book ai didi

Java、Jacob 和 Microsoft Word : how to properly handle events?

转载 作者:行者123 更新时间:2023-12-01 12:46:40 30 4
gpt4 key购买 nike

我的目标是编写一个java小程序应用程序,该应用程序在客户端计算机上的临时目录中写入一个word文档(该文档是从数据库中获取的),并使用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文档的内容以及如何检测应用程序关闭事件

最佳答案

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

关于Java、Jacob 和 Microsoft Word : how to properly handle events?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24639627/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com