0

I am writing a program to read some text or textfields from a Microsoft Office Word document and replace it with new words using Jacob. I got the help from this link http://tech-junki.blogspot.de/2009/06/java-jacob-edit-ms-word.html but it didn't work. Could you please help me by telling me how can I read some text and replace it with new text!? If you have a better idea, please tell me.

Note:

1- This method didn't give me any error but couldn't find the speciffic words!

2- How can I write an If() to know if our requested Search text (in this method arrayKeyString) exists or is written in ms word?

Thanks.

    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);
    }
Cyber Storm
  • 217
  • 1
  • 13
A R
  • 425
  • 1
  • 5
  • 11

2 Answers2

0

ok I have also modified your for loop which had logical errors, As far as i can understand your question you don't need an if statement if you are trying to replace all the words from your hashmap in the document:

//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);
}
user_CC
  • 4,686
  • 3
  • 20
  • 15
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.

griFlo
  • 2,084
  • 18
  • 28
  • 1
    Because, like in the comment of A R said, the other solution just works for one replacement. This code goes throug the whole map and replaces for each element ALL of the occurrences in the word Document. – griFlo Feb 16 '15 at 16:44