在Word中读取单词并用新单词替换(使用JAVA)

时间:2013-04-16 13:59:22

标签: java java-ee ms-word ms-office jacob

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

2 个答案:

答案 0 :(得分:0)

好的我还修改了你的for循环,它有逻辑错误,只要我能理解你的问题,你不需要if语句,如果你试图替换文档中hashmap的所有单词:

//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);
}

答案 1 :(得分:0)

我知道我的回答可能有点太晚了,但是我会把这个留在这里,给所有能找到这个页面的人。

这就是我如何做到这一点:

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();
 }
}

此代码遍历整个地图,并替换单词Document中所有出现的每个元素。