Saturday, June 9, 2012

How to Edit Microsoft Word Documents Programmatically

In many cases, we need to edit existing word documents that follow a pattern. Doing it manually is not a good idea if the word document has be edited often. An example is that a release notes word document which needs to be updated for every release but not all the contents require change. The change may be required to update certain information such as release deliverable version numbers, date, numbers of bugs fixed etc. If number of deliverables is more, its time consuming to update the word document manually.

There are ways to create a new word document altogether in Java programming language (POI), however editing existing document is not a straight forward task. So use of Microsoft technologies will ease the process since when word document is edited, the existing formatting needs to be retained.

We can use VBScript to edit word document and this makes it much easier. Lets look at an example of editing Microsoft word document using VBScript and the VBScript can be invoked through a Java Program or any scripting language.

Simple example: VBScript (EditTable.vbs)  
---------------------------------------------------------
| Set wd = CreateObject("Word.Application")
| Set doc = wd.Documents.Open ("d:\work\ReleaseNotes.doc")
| x = 2  'Second row of the table
| Set objTable = doc.Tables(4)    'I want to edit 4th table in the release notes document
| objTable.Cell(x, 2).Range.Text = "DATA1"  'DATA1...n is a place holder & will be replaced by external |program like java program
| objTable.Cell(x, 3).Range.Text = "DATA2" 
| objTable.Cell(x, 4).Range.text = "DATA3"
| objTable.Cell(x, 5).Range.text = "DATA4"
| doc.save()
-------------------------------------------------------

In above VBScript snippet, the DATA1, DATA2...etc are the new values to the respective table cells. The script can be enhanced to read these data from a database or any other source.
The above script can be called through following Java Code.
----------------------------------------------------------------------------
| public class WriteFile {
|    public static void main(String[] args){
|        try{
|            Process p = Runtime.getRuntime().exec("cmd /c d:/work/EditTable.vbs");
|            p.waitFor();
|            int i = p.exitValue();
|            System.out.println("Exit value="+i);
|        }catch(Exception e){
|            e.printStackTrace();
|        }
|   }
|}
--------------------------------------------------------------------------

If you want to learn in depth on how to create new word documents, Microsoft Excel Sheet files, etc, have a look at  Apache POI - the Java API for Microsoft Documents.

2 comments:

  1. Below is the link of the page which i have found during browsing for code for editing java files and this page has been very useful for me because this library offers many features to java developer like me. You should try it also.

    http://www.aspose.com/docs/display/wordsjava/Feature+Overview

    ReplyDelete
  2. There is another Java API for Word known as Aspose.Words for Java using which you can create, edit, read and convert word docs to many other formats.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...