在程序中使用jacob打开了word文件,代码如下所示:public class test{
public static void main(String[] args) {
WordBean word=new WordBean();
word.openWord(true);
word.createNewDocument();
word.insertText("Hello word.");
}
}class WordBean extends java.awt.Panel {
private ActiveXComponent MsWordApp = null;
private Dispatch document = null;
public WordBean() {
super();
}
public void openWord(boolean makeVisible) {
//Open Word if we've not done it already
if (MsWordApp == null) {
MsWordApp = new ActiveXComponent("Word.Application");
}
//Set the visible property as required.
Dispatch.put(MsWordApp, "Visible",
new Variant(makeVisible));
}
public void createNewDocument() {
//Find the Documents collection object maintained by Word
Dispatch documents =
Dispatch.get(MsWordApp,"Documents").toDispatch();
//Call the Add method of the Documents collection to create
//a new document to edit
document = Dispatch.call(documents,"Add").toDispatch();
}
public void insertText(String textToInsert) {
// Get the current selection within Word at the moment. If
// a new document has just been created then this will be at
// the top of the new doc
Dispatch selection =
Dispatch.get(MsWordApp,"Selection").toDispatch();
//Put the specified text at the insertion point
Dispatch.put(selection,"Text",textToInsert);
}
public void saveFileAs(String filename) {
Dispatch.call(document,"SaveAs",filename);
}
public void printFile() {
//Just print the current document to the default printer
Dispatch.call(document,"PrintOut");
}
public void closeDocument() {
// Close the document without saving changes
// 0 = wdDoNotSaveChanges
// -1 = wdSaveChanges
// -2 = wdPromptToSaveChanges
Dispatch.call(document, "Close", new Variant(0));
document = null;
}
public void closeWord() {
Dispatch.call(MsWordApp,"Quit");
MsWordApp = null;
document = null;
}
}能够打开一个word文件了,但是问题也来了,当手动关闭word文件时(而不是调用closeWord函数),
会弹出一个对话框,提示"此程序正由另一应用程序或用户使用.(c:\...\Normal.dot)",
点击"确定"后弹出一个"另存为"的对话框.
此时系统存在若干个"winword.exe"进程.有谁知道怎么解决吗?
谢谢!
public static void main(String[] args) {
WordBean word=new WordBean();
word.openWord(true);
word.createNewDocument();
word.insertText("Hello word.");
}
}class WordBean extends java.awt.Panel {
private ActiveXComponent MsWordApp = null;
private Dispatch document = null;
public WordBean() {
super();
}
public void openWord(boolean makeVisible) {
//Open Word if we've not done it already
if (MsWordApp == null) {
MsWordApp = new ActiveXComponent("Word.Application");
}
//Set the visible property as required.
Dispatch.put(MsWordApp, "Visible",
new Variant(makeVisible));
}
public void createNewDocument() {
//Find the Documents collection object maintained by Word
Dispatch documents =
Dispatch.get(MsWordApp,"Documents").toDispatch();
//Call the Add method of the Documents collection to create
//a new document to edit
document = Dispatch.call(documents,"Add").toDispatch();
}
public void insertText(String textToInsert) {
// Get the current selection within Word at the moment. If
// a new document has just been created then this will be at
// the top of the new doc
Dispatch selection =
Dispatch.get(MsWordApp,"Selection").toDispatch();
//Put the specified text at the insertion point
Dispatch.put(selection,"Text",textToInsert);
}
public void saveFileAs(String filename) {
Dispatch.call(document,"SaveAs",filename);
}
public void printFile() {
//Just print the current document to the default printer
Dispatch.call(document,"PrintOut");
}
public void closeDocument() {
// Close the document without saving changes
// 0 = wdDoNotSaveChanges
// -1 = wdSaveChanges
// -2 = wdPromptToSaveChanges
Dispatch.call(document, "Close", new Variant(0));
document = null;
}
public void closeWord() {
Dispatch.call(MsWordApp,"Quit");
MsWordApp = null;
document = null;
}
}能够打开一个word文件了,但是问题也来了,当手动关闭word文件时(而不是调用closeWord函数),
会弹出一个对话框,提示"此程序正由另一应用程序或用户使用.(c:\...\Normal.dot)",
点击"确定"后弹出一个"另存为"的对话框.
此时系统存在若干个"winword.exe"进程.有谁知道怎么解决吗?
谢谢!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货