Thursday, June 4, 2009

Java Jacob - Edit MS Word Templates/Documents 97 - 2003

In this post we will discuss about editing word templates, using the following word features:
  • Bookmarks
  • Tables
  • Search text
Prerequisite:
  • You need to have word installed.
  • Download JaCoB jar and dll - "Java Com Bridge is an open source library to talk with COM api's using Java native calls".
  • Add Jacob dll to PATH environment variable.
  • Add Jacob Jar to your java project classpath


Let get started. Add the following import statements.
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;


First we need to run/instantiate the word application.
ActiveXComponent word = new ActiveXComponent("Word.Application");
word.setProperty("Visible", new Variant(true));

// word.setProperty("Visible", new Variant(false));
if you want the word application to be invisible

Open an existing document/template.

String fileName = “c:\\sample.dot”;
Dispatch documents = word.getProperty("Documents").toDispatch();
//documents is a handle for all the open documents in word application
Dispatch document = Dispatch.call(documents, "Open", fileName).toDispatch();
//document is reference to sample.dot

Now we are ready to set text for a bookmark. It is a piece of cake believe me :)

String name = "name of your bookmark"; //name of the bookmark in template
String value = "text you want to enter there :) "; //text you want to put there

Dispatch obj = Dispatch.call(document, "Bookmarks", name).getDispatch();
// get a handle to the bookmark

Select the range (as per my understanding a selected area/text in document
, like the text you selected for adding bookmark).

Dispatch d = Dispatch.call(obj, "Range").getDispatch();
Dispatch.put(d, "Text", value);
//set desired text.

Now we will search for some text in the document.

String searchText = "your search text";
Dispatch selection = word.getProperty("Selection").toDispatch();
Dispatch find = Dispatch.call(selection, "Find").toDispatch();
Dispatch.put(find, "Text", searchText);

Dispatch.call(find, "Execute");

Lets write a single line of text after the text we just searched.

String singleLineText = "your text goes here";
boolean startFromNewLine = true;
if (startFromNewLine) {

Dispatch.call(selection, "TypeParagraph");
}
Dispatch.put(selection, "Text", singleLineText);
Dispatch.call(selection, "MoveDown");


Now most interesting part we are going to play with tables.

First of all lets select a table from doc/template. Word recognizes tables by table number (index of table in a list/array of all tables in the document) . This table number starts from 1 and increases as you add more tables to the document. So if a table has index of 5 then the index of table you next will be 6.

int tableNumber = 1; //first table in document.
Dispatch table = Dispatch.call(document, "Tables", new Variant(tableNumber)).getDispatch();


In the code above "Tables" is a kind of handle for all the tables in doc. We can even add tables from code using this handle.

Now lets insert some text in cells of the table we just selected. Again you need to keep in mind like table starts with index of 1 in collection of tables, rows and columns follow the same behavior.
So first row index is 1 and fist column is 1.

We are going to select first cell of table, cell 1,1
Dispatch cell = Dispatch.call((Dispatch)table, "Cell", new Variant(1), new Variant(1)).getDispatch();
Now select the range of cell and put the text you want.
Dispatch range = Dispatch.call(cell, "Range").getDispatch();
Dispatch.put(range, "Text", "your text goes here");



So we can insert text we want to, in any cell of a table. Now lets try adding rows to a table.
Dispatch rows = Dispatch.call((Dispatch)table, "Rows").getDispatch();
Dispatch.call(rows, "Add");
yeah!! we just added another row.


Now we need to save document and close word application. That's easy all we need to do is add a few lines.
Dispatch wordBasic = Dispatch.call(word, "WordBasic").getDispatch(); //Save and close file Dispatch.call(wordBasic, "FileSaveAs", targetFileName);
Dispatch.call(document, "Close", new Variant(true));

//Quit Word.
word.invoke("Quit", new Variant(false));

So now we have successfully edited and saved a document from Java. :)

This is my first blog/post ever. Any suggestion is highly appreciated.

You can reach me at ratnesh dot m at s7software.com