【问题标题】:Reading words in Ms Word and replacing it with new words (with JAVA)阅读 Ms Word 中的单词并将其替换为新单词(使用 JAVA)
【发布时间】: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


【解决方案1】:

好的,我还修改了您的 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);
}

【讨论】:

    【解决方案2】:

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

    这就是我的做法:

    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 中出现的所有事件。

    【讨论】:

    • 因为,就像在 A R 的评论中所说的那样,另一种解决方案只适用于一个替代品。此代码遍历整个地图,并为每个元素替换单词 Document 中出现的所有事件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多