Skip to content

Getting Started with COM

Keegan Witt edited this page Jul 23, 2015 · 2 revisions

Code written with Scriptom looks great and is easy to read. Sure. But it may not be as easy to write as it is to read. Where is the look-ahead help? Where is the integration with your favorite IDE? How do you get started writing code against COM objects, many of which have little or no online documentation? In this rather short article, I talk about why Scriptom does not have IDE integration, along with some techniques and tools you can use to make COM programming with Scriptom much easier.

No IDE Integration for Scriptom

Let's start with IDE integration. To make it short, there isn't any, and it isn't planned. The trouble is, Scriptom uses "late bound" COM calls, exclusively. Every COM object in Scriptom is just an ActiveXObject to Groovy. Even at runtime, there is no reliable way to determine exactly what kind of an object you are working with under the covers. Scriptom just passes the method name, along with the arguments, to Windows - and trusts that everything will go well.

For those of us who are used to static typing, this may seem like a dangerous practice. I think you will find, though, that it works surprisingly well. It also simplifies things considerably. Have you seen the number of COM wrappers that Microsoft.NET has to support (for some, multiple versions of the same library)? Scriptom doesn't require a COM wrapper generator (though it does include constant definitions for some COM libraries). You can just write your code and run it. And it just works.

Scriptom uses the same calling mechanism that is used by Microsoft's typeless scripting languages, like VBScript and JScript. It is also the calling convention that is used from Visual Basic 6 or VBA when you are working with an Object. That is why they call it Scriptom. It uses the calling convention that is normally used by scripting languages.

An Easier Way than Trial and Error

So if there is no IDE integration for Scriptom, how do you figure out the COM thing? Here are some ideas, based on my own experience working with COM. These aren't quite the same as having full IDE integration, like Java in Eclipse, but in my experience they do help you get the job done.

Use VB6 or VBA to browse the object models and write sample code. Visual Basic 6 and VBA have a nearly identical set of tools for browsing objects, setting references to COM libraries, and for look-ahead help. Most COM libraries were designed to be compatible with Visual Basic, and those will usually work well with Scriptom. VBA is available as the macro language in all the Microsoft Office products, and in many other products as well. Alternately, use Visual Studio.NET to browse the COM object model. I don't recommend this over VB6/VBA because the support for COM is a little more obscure in the Microsoft.NET products. However, in a pinch, it works, and the Visual Studio Express products are free. Visual Basic 6 is part of Visual Studio 6, which was desupported by Microsoft in 2008. Visual Studio 6 still works if you have it, but if you don't, you can't get it. Microsoft has removed it and all the associated documentation from their website. Buy a book. There are many books available that cover COM APIs for specific products. For example, a used copy of Excel 2002 VBA on Amazon would run you less the $15, including shipping. When you are working with the really complex applications, like Excel and Word, I have always found it beneficial to have a second opinion. Use the web to find examples. Some people call it "Programming by Google." If you want to do something, you can bet that some other bloke has already done it and published it somewhere on the web. Read the manual. Use Microsoft's search tool at http://msdn.microsoft.com to find the official documentation. Some of it is very good, some it will leave you with questions, but all of it is official. Don't write code, write a macro. For Microsoft Office applications that support macros, the macro language is VBA. This means that when you create a macro, Word or Excel will actually write the code for you. It is usually trivial to port VBA to Groovy/Scriptom. This is an excellent way to figure out how to do obscure tasks in Office. There. That should get you started off on the right foot. Enjoy!

This article was consolidated from ideas developed in an email thread in the Groovy users list. Thanks to Thomas Elmiger and A. Tres Finocchiaro.