COM Scripting With Groovy

I’ve mentioned before my use of Groovy in Groovy Closures and Common Groovy Errors. I have also mentioned the need to use Visual Basic scripts to open MS Word, or print an HTML document from Internet Explorer. In this article I will mention how to use Groovy and Scriptom to do COM scripting. Before I integrated with Scriptom I had to write one off Visual Basic scripts and run them from Java by using the Runtime’s exec(String) method. Now I could write the COM script in Groovy. Here is an example of Groovy code that will open a new Outlook mail message window and populate the To field:

import org.codehaus.groovy.scriptom.ActiveXProxy

def outlook = new ActiveXProxy("Outlook.Application");
def message = outlook.CreateItem(0);
def emails = "user1@domain1.com;user2@domain2.com";
def rec = message.Recipients.add(emails);
rec.Type = 1 // To = 1, CC = 2, BCC = 3
message.Display(true);

You can run this code from the groovyConsole once you have Scriptom configured.

Technorati Tags: , , , , ,