Accessing Microsoft Word from Java

Java/J2EE COM Interoperability Products Page

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

Contents

  1. Introduction
  2. Run the Java Client on Local Windows Machine
    1. Generate Java Proxies
    2. Create the Example
    3. Compile and Run the example
  3. Run the Java Client on Remote Machine, e.g. Windows, UNIX, Linux and etc
  4. More about Word programming

1 Introduction

This example shows you how to programmatically automate Microsoft Word from Java using the COM API that Word exposes, in order to replace a phrase in a Word document. You can run the Java client on a Windows machine to access its local Microsoft Word, or run the Java client on a non-Windows machine (such as Linux) to access Microsoft Word installed on a remote Windows machine.

We assume that you are familiar with Java -- no COM knowledge should be required. We assume you have downloaded and expanded the J-Integra® kit from http://j-integra.intrinsyc.com/ and installed it correctly.

2 Run the Java Client on Local Windows Machine

You can try this example on local Windows machine first to get a feel for how easy it is to use J-Integra® to access Microsoft Word from Java. Once you make it working on local machine, you can then try to run the Java client on a non-Windows machine to remotely access Microsoft Word on another Windows machine.

2.1 Generate the Java proxies

Run J-Integra®'s com2java tool on the Windows machine, and select C:|Program Files|Microsoft Office|OFFICE11|MSWORD.OLB as the type library, choose an empty directory named msword (e.g. C:\msword) as output directly, and use msword as Java package name. Click the Generate Proxies button to generate Java proxies from Word type library.

2.2 Create the example

This example is for Microsoft Office Word 2003. If you use a different version of Word, you need to look at the generated Java proxies (Documents.java and Find.java) and call the methods accordingly.

Create a word document called "c:\temp\test.doc" with the contents "how now brown cow" - after running the example the file contents should have changed.

Create a .java file named WordExample.java. Then copy and paste the Java code below:

import com.linar.jintegra.AuthInfo;
import msword.*;

public class WordExample {

  public static void main(java.lang.String[] args) {
    String oldfilestr = "c:\\temp\\test.doc";

    try {
      // DCOM authentication: Make sure Nt Domain, Nt User, Nt Password are valid credentials.
      // Uncomment this line if WordExample.java remotely accesses Word:
      // com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "NT USER", "NT PASSWORD");

      // Specify host name or IP address of Word machine as parameter if 
      // WordExample.java remotely accesses Word.
      // Application doc = new Application("123.456.789.0"); 
      Application app = new Application();

      java.lang.System.out.println("The version is: " + app.getVersion());
      java.lang.System.out.println("The path is: " + app.getPath());

      app.setVisible(true);   // Nice to see what is happening

      Documents docs = app.getDocuments();

      // For Word 2003, use:
      _Document document = docs.open(
        oldfilestr,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null
      );
      // For Word XP, use:
      // _Document document = docs.open(
      //   oldfilestr,
      //   null,
      //   null,
      //   null,
      //   null,
      //   null,
      //   null,
      //   null,
      //   null,
      //   null,
      //   null,
      //   null,
      //   null,
      //   null,
      //   null
      // );
      // For Word 2000, use:
      // _Document document = docs.open(
      //   oldfilestr, // FileName,
      //   null,       // ConfirmConversions
      //   null,       // ReadOnly
      //   null,       // AddToRecentFiles
      //   null,       // passwordDocument
      //   null,       // password template
      //   null,       // revert
      //   null,       // write password document
      //   null,       // write password template
      //   null        // format
      // );

      Range rg = document.getContent();
      Find fd = rg.getFind();

      Thread.sleep(2000);

      // Replace (default replaces 1 phrase at a time)
      // For Word XP or 2003, use:
      fd.execute(
        "ow",
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        null,
        "igh",
        null,
        null,
        null,
        null,
        null
      );
      // For Word 2000, use:
      // ff.execute(
      //   "ow", // FindText
      //   null,  // MatchCase
      //   null,  // MatchWholeWord
      //   null,  // MatchWildcards
      //   null,  // MatchSoundsLike
      //   null,  // MatchAllWordForms
      //   null,  // Forward
      //   null,  // Wrap
      //   null,  // Format
      //   "igh", // ReplaceWith
      //   null
      // );       

      Thread.sleep(2000);
      app.quit(new Integer(WdSaveOptions.wdSaveChanges), null, null);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // Release all the COM objects:
      com.linar.jintegra.Cleaner.releaseAll();
    }
  }
}

The above Java code is based on and equivalent to the VB code below:

Dim app As Word.Application
Dim docs As Word.Documents
Dim doc As Word.Document
Dim oldfilestr As String
Dim rg As Word.Range
Dim fd As Word.find

oldfilestr = "c:\\temp\\test.doc"

Set app = New Application
app.Visible = True
Set docs = app.Documents
Set doc = docs.Open(oldfilestr)
Set rg = doc.Content
Set fd = rg.find
fd.Execute "ow", , , , , , , , , "igh"
app.Quit WdSaveOptions.wdSaveChanges

2.3 Compile and run the example

On the Java client machine, make sure your CLASSPATH and PATH environment variables are set up according to J-Integra® installation instructions. Compile and run the example in  J-Integra®'s native mode (you need to use DCOM mode if remotely accessing Word):

javac WordExample.java
java -DJINTEGRA_NATIVE_MODE WordExample

If the method Find.execute(...) fails with AutomationException 0x800706f7 or Run-time error 430, follow the resolution mentioned in Microsoft KB article 292744 BUG: Automation client receives error or crashes calling Word's Find object.

3 Run the Java Client on Remote Machine, e.g. Windows, UNIX, Linux and etc

You can also run the Java client on a remote machine, such as Linux, Solaris, UNIX and AIX. For instance, if you run it on a Linux machine, then you must do the following.:

  1. Move the com2java tool to the Windows machine to generate the Java proxies from Word, and then move the Java proxies from the Windows machine to the Linux machine.
  2. Install J-Integra® (the jintegra.jar file) on the Linux machine and include the jintegra.jar file and generated Java proxies in CLASSPATH.
  3. Use DCOMCNFG to configure Word on the Windows machine.
  4. Pass correct login credentials to com.linar.jintegra.AuthInfo.setDefault("NT DOMAIN", "NT USER", "NT PASSWORD");
    Refer to Configuring DCOM for Remote Access for more information about AuthInfo.setDefault.
  5. Pass the IP address or computer name of the Word machine to the constructor of Applicaiton object in WordExample.java:
    Application doc = new Application("123.456.789.0");
  6. Move WordExample.java to the Linux machine. Compile and run it in DCOM mode without using DJINTEGRA_NATIVE_MODE property:
    java WordExample

4 More about Word programming

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. It's also easier to find a VB example first, and then convert the VB example to Java program using the reference Mapping VB Code to Java Code.