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: , , , , ,


4 Responses to “COM Scripting With Groovy”

  • The Don Says:

    Needed to print from Word, Excel etc. using Java.
    Thanks for putting me on the right track with Groovy and Scriptom.

  • vp Says:

    How do you know the API for making these COM calls to Outlook? (e.g. CreateItem(), message.Recipients.add())
    Is there any helpful documentation or reference that you use to find the available methods?
    Thanks

  • TechKnow Says:

    @vp – Thanks for the comments. That is a really good question. The above Groovy/COM code is very similar, if not identical, to how you would do it in Visual Basic. Even the naming convention is preserved from how you do it in VB. I recommend you try to find vbs code to do what you want and then try to convert it to Groovy. You should be able to find a lot of vbs code examples. Hope that helps.

  • vp Says:

    I see, so we can use available VB documentation, but stay within our Java/Groovy comfort zone. Very cool!

    Still, are there any VB references you could recommend for us?

    Sorry to be a pest, but I’m lost when it comes to MS web resources & books.
    Thanks much!

Leave a Reply