2013-04-16 59 views
0

我正在编写一个程序,用于从Microsoft Office Word文档中读取一些文本或文本字段,并使用Jacob将其替换为新单词。 我从这个链接http://tech-junki.blogspot.de/2009/06/java-jacob-edit-ms-word.html得到了帮助,但它没有奏效。你能帮我告诉我如何阅读一些文本并用新文本代替它!? 如果你有更好的主意,请告诉我。阅读Ms Word中的单词并用新单词替换它(使用JAVA)

注:

1-这种方法并没有给我任何错误,但找不到speciffic话!

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); 
    } 
+0

你上面的进展如何?或者你有什么问题? –

+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); 
} 
+0

但你说得对,我们不需要If()! –

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

这一代码进入通过量的整个地图,并替换每个元素中的所有Word文档中的出现的。

+0

请为你使用该代码的原因添加一些解释,比其他人... –

+1

因为,就像在A R的评论中说的,另一种解决方案只适用于一个替换。此代码通过整个映射并替换单词Document中的每个元素的所有事件。 – griFlo