Word Automation with Groovy

I have been working with Ruby and PHP for a bit of automation, but I thought I would give Groovy a shot. It turns out Groovy is just as easy.

First follow the steps at CodeHaus to install Scriptom. Then create a groovy file and paste this in.

package com.scottpreston.groovy.com;

import org.codehaus.groovy.scriptom.*;

def write_word() {

	def word = new ActiveXObject("Word.Application");
	word.Visible = 1;
	word.Documents.Add();
	word.Selection.PageSetup.LeftMargin = '2"';
	word.Selection.PageSetup.RightMargin = '2"';

	//Setup the font
	word.Selection.Font.Name = 'Verdana';
	word.Selection.Font.Size = 8;

	//Write some text
	word.Selection.TypeText("This is a test document");

	//quit and release COM resources
	word.quit();
	//word.Release();
	word = null;

}

write_word();