【发布时间】:2013-04-16 13:59:22
【问题描述】:
我正在编写一个程序来从 Microsoft Office Word 文档中读取一些文本或文本字段,并使用 Jacob 将其替换为新词。 我从这个链接http://tech-junki.blogspot.de/2009/06/java-jacob-edit-ms-word.html 得到了帮助,但它没有用。你能告诉我如何阅读一些文本并用新文本替换它吗? 如果你有更好的想法,请告诉我。
注意:
1-这个方法没有给我任何错误但是找不到具体的词!
2- 我如何编写一个 If() 来知道我们请求的搜索文本(在此方法中为 arrayKeyString)是否存在或以 ms 字编写?
谢谢。
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
//class
ActiveXComponent oWord = null;
Dispatch documents = null;
Dispatch document = null;
Dispatch selection = null;
//method
oWord = new ActiveXComponent("Word.Application");
documents = oWord.getProperty("Documents").toDispatch();
document = Dispatch.call(documents, "Open", finalName).toDispatch();
Dispatch selections = oWord.getProperty("Selection").toDispatch();
Dispatch findT = Dispatch.call(selections, "Find").toDispatch();
//hm is a Hashmap
for (int i=0; i<hm.size();i++){
hm.get(array[i].toString());
String arrayValString = (arrayVal[i].toString());
String arrayKeyString = array[i].toString();
// Here we should write an if() to check for our key word:
Dispatch.put(findT, "Text", arrayKeyString);
Dispatch.call(findT, "Execute");
Dispatch.put(selections, "Text", arrayValString);
}
【问题讨论】:
-
您在上述方面的进展如何?或者你遇到了什么问题?
-
它找不到单词并替换它们!它没有给我任何错误!
标签: java jakarta-ee ms-word ms-office jacob