Accessing Microsoft Word VBA from Java

Java/J2EE COM Interoperability Products Page

This example demonstrates how to access Microsoft Word VBA from Java. J-Integra® for COM is a Java interoperability component that bridges Java and Microsoft Word VBA. It provides bi-directional access of Java objects and COM components.

We do not provide the documentation of the generated Java proxies since the Java proxies are just mapped from the programming API of the COM component. For more information about Microsoft Word programming, please refer to the Microsoft Word VBA Language Reference. The quickest way to start programming Java-to-Word applications is to find a VB example first, and then map the VB code to Java.

Refer to the Excel VBA example for the details of steps.

// This example invokes the Visual Basic method 'method1' in the template associated with a
// Word document.
//
// Prior to running this example define the VBA 'method1' in the default Word 
// template's 'ThisDocument' object:
// o Start Word.
// o Click on Tools|Macro|Visual Basic Editor
// o In the Project window, expand Normal->Microsoft Word Objects->ThisDocument
// o Double-click on 'ThisDocument' to edit its code.
// o Insert the following method:
//    Public Function method1(ByVal p1 As String, ByVal p2 As Date) As Double
//       ActiveDocument.Content.InsertBefore (p1 & " " & p2)
//       method1 = 1234.5678
//     End Function
// o Click on File|Save Normal

public class WordDynamicInvoke {

  public static void main(java.lang.String[] args) {
    try {

      // If running Jvm on same machine as Word, then leave the AuthInfo.setDefault() line
      // commented, and put J-Integra®'s 'bin' directory in your PATH to allow J-Integra® to
      // use native code to ascertain and use your current login identity.
      // If running JVM on a UNIX machine, uncomment the line, configure DCOM access to
      // Microsoft Word on the Windows machine, and pass the Windows machine's IP name as
      // a parameter to the word.Application constructor
      // com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "USER", "PASSWORD");

      // Start Word and make it visible
      word.Application app = new word.Application();
      app.setVisible(true);

      // Add a document
      word.Documents docs = app.getDocuments();
      word.Document doc = docs.add(null, null);

      // Set up Object array of parameters to be passed to the method.
      // Only passing parameters by value is currently supported.
      Object[] params = { "hello", new java.util.Date() };

      // Invoke the method "method1" -- it returns a double, which J-Integra® wraps in the
      // corresponding primitive type wrapper.
      java.lang.Double retVal = (java.lang.Double) doc.invokeMethodByName("method1", params);
      System.out.println("Return Value is " + retVal);

      // Sleep 5 seconds so that you can see the Word document's content has been
      // modified to include the string and date passed as parameters to the method.
      Thread.sleep(5000);

      // Quit -- don't save changes
      app.quit(new Integer(word.WdSaveOptions.wdDoNotSaveChanges), null, null);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      com.linar.jintegra.Cleaner.releaseAll();
    }
  }
}