René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -

Automating Microsoft Word with Java (and J-Integra)

Word and Excel can be automated, that is, their rich functionality is exposed through a COM interface and can be accessed from any other language that speaks COM. I have already written about this: C++ and MS Word, Perl and MS Word, Excel and MS Word and PL/SQL and MS Word. J-Integra promises to make it possible to make COM interfaces accessible for Java as well.
I wanted to test how easy or hard it is to automate Word with J-Intagra. Here's what I did:
First, I created a directory in which I'd create my test Java class and into which I'd also store the Java wrapper classes that will be created by J-Integra.
C:\>mkdir \jcom
C:\>cd \jcom
I've installed J-Integra into C:\rene\J-Integra. So, I add J-Integra's bin directory to my PATH environment variable and start J-Integra (com2java.exe):
C:\>set PATH=%PATH%;C:\rene\J-Integra\com\bin
com2java asks for a type library. In short, a type library contains a binary description of an interface exposed by a COM-component. In this sense, they contain the same information that is contained in an IDL (Interface Definition Language) file. J-Integra needs this information to create the wrapper classes.
As I want to automate MS Word, I chose the following type library: C:\Program Files\Microsoft Office\Office10\MSWORD.OLB. (If you're following this example, you must have MS Word installed to have its type library and its location is obviously dependant on the installation directory of Word).
com2java asks then for the name of the Java package in which the wrapper files should go. I chose WordWrapper as the package name.
It would be possbile to change some options. I left the default unchanged.
I then Clicked Generate Proxies... and choose as folder: jcom\WordWrapper. Note that WordWrapper must match the name of the package name chosen.
The generated files include:
  • A Java interface and proxy class for each COM interface.
  • A Java class for each COM class.
  • A Java interface for each ENUM (a set of constant definitions).
773 .java files were generated into c:\jcom\WordWrapper
It was then about time to write a little Java application that makes use of the generated wrapper classes:
import WordWrapper.*; // Must match name of package
import com.linar.jintegra.*;
import java.lang.*;
public class JavaToWordTest {
  public static void main(String[] args) {
    try {
      Application word = new Application();

      Document  document  = word.getDocuments().Add(null, null, null, null);
      Selection selection = word.getSelection();

      selection.TypeText("Hello World");

      selection.TypeText("How do you feel today");

      selection.TypeText("Some header");
      selection.setStyle("Heading 1");

      Style style =  document.getStyles().Item("Heading 1");
      Font font=style.getFont();

      // uncomment to quit without saving
      // word.Quit(new Boolean(false), null, null);
    catch(Exception e){
C:\jcom>javac -J-mx128m -J-ms128m -classpath .;\rene\J-Integra\com\lib\jintegra.jar 
Trying to run the application:
C:\jcom>java JavaToWordTest