0

我正在编写一个程序来从 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);
    }
4

2 回答 2

0

I know it is probably a little bit too late for my answer, but i'll leave this here, for all the others who will find this page.

This is how i've done this:

private static final Variant MATCH_CASE = new Variant(true);
private static final Variant MATCH_WILDCARDS = new Variant(false);
private static final Variant FORWARD = new Variant(true);
private static final Variant MATCH_WHOLE_WORD = new Variant(false);
private static final Variant MATCH_SOUNDS_LIKE = new Variant(false);
private static final Variant MATCH_ALL_WORD_FORMS = new Variant(false);
private static final Variant FORMAT = new Variant(false);

private static final Variant WRAP = new Variant(1);
private static final Variant REPLACE = new Variant(2);    

//...........


Dispatch selection = Dispatch.get(oleComponent,"Selection").toDispatch();
Dispatch oFind = Dispatch.call(selection, "Find").toDispatch();



for (Entry<String, String> entry : replacements.entrySet()) {

 while (replaced) {
     Variant variant = Dispatch.invoke(oFind,"Execute",Dispatch.Method, new Object[] {entry.getKey(),MATCH_CASE, MATCH_WHOLE_WORD,MATCH_WILDCARDS, MATCH_SOUNDS_LIKE, MATCH_ALL_WORD_FORMS,FORWARD, WRAP, FORMAT, entry.getValue(), new Variant(true), REPLACE }, new int[1]);

   replaced = variant.getBoolean();
 }
}

This code goes throug the whole map and replaces for each element ALL of the occurrences in the word Document.

于 2015-02-16T16:34:17.863 回答
0

好的,我还修改了您的 for 循环,它有逻辑错误,据我所知,如果您尝试替换文档中哈希图中的所有单词,则不需要 if 语句:

//hm is a Hashmap 
for (int i=0; i<hm.size();i++){
   //you were getting the value to be replaced but not storing that in arrayValString object
   String arrayValString = hm.get(array[i].toString());
   String arrayKeyString = array[i].toString();
   // Here we should write an if() to check for our key word:
   //if you want to replace all the text in your hash map in the word document then you don't need a if condition ...so if the text is not present in the document nothing will be replaced.

   Dispatch.put(findT, "Text", arrayKeyString); 
   Dispatch.call(findT, "Execute"); 
   Dispatch.put(selections, "Text", arrayValString);
}
于 2013-04-17T09:26:31.433 回答