0
votes

I added a VBA procedure (saveasdoc) to Normal.dot.

I have tested it with a .docx file:

  1. Saves the opened file with same name to a new .doc file.
  2. Exits the application MS Word.

I want to run this VBA procedure from a Java Application using the JACOB library.

Here is what I have tried :

System.out.println("Start!!");
File docfile = new File("C:/test.docx");
ActiveXComponent word = new ActiveXComponent("Word.Application");
word.setProperty("Visible", new Variant(true));
Dispatch oDocuments = word.getProperty("Documents").toDispatch();
Dispatch.call(oDocuments, "saveasdoc()", docfile.getPath()).toDispatch();
System.out.println("End!!");

The MS Word app is shown and the document is loaded, but the only claimed macro executor code is generating an exception :

SEVERE: null
com.jacob.com.ComFailException: Can't map name to dispid: saveasdoc()
  at com.jacob.com.Dispatch.invokev(Native Method)
  at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
  at com.jacob.com.Dispatch.invokev(Dispatch.java:625)
  at com.jacob.com.Dispatch.callN(Dispatch.java:453)
  at com.jacob.com.Dispatch.call(Dispatch.java:541)
  at WordProcessing.main(WordProcessing.java:30)
1

1 Answers

0
votes

You want Application.Run (http://msdn.microsoft.com/en-us/library/aa220716(v=office.11).aspx)

Not tested but the general direction you should be pursuing:

System.out.println("Start!!");
File docfile = new File("C:/test.docx");
ActiveXComponent application = new ActiveXComponent("Word.Application");
Dispatch.call(application, "Run", new Variant("saveasdoc"), new Variant(docfile.getPath()));
System.out.println("End!!");