Creating Automation Objects

Unfortunately, automation is one of the areas in which the ActiveState distribution differs slightly from the libwin32 OLE module for use with the standard Perl distribution. Both distributions use the CreateObject function to create an automation object, but the syntax (and module name) is slightly different:

# ActiveState distribution
use OLE;
$obj = CreateObject OLE "Excel.Application" || 
        die "CreateObject: $!";

# libwin32 Win32::OLE
use Win32::OLE;
Win32::OLE::CreateObject("Excel.Application", $obj) || 
        die "CreateObject: $!";

The ActiveState CreateObject takes two arguments: a class type (currently, always OLE), and a ProgID (program ID) string of the object to create. When an automation server is registered on the system, it stores a CLSID (class ID), which is a token that uniquely identifies an OLE object, and a ProgID that provides a human readable way to access the CLSID. Perl does the conversion internally, so you just need to provide the ProgID. A server generally has two types of ProgIDs: one is a version-independent ProgID that typically identifies the most current version of the server, the other is a version-specific ProgID that denotes a specific application version.

Here are some examples of ProgIDs that you might use:

Excel.Application (Microsoft Excel Application Object)
Excel.WorkSheet   (Microsoft Excel Worksheet Object)
Word.Document.8   (Microsoft Word Document Object, Ver 8)
Word.Basic.8      (Microsoft WordBasic Object, Ver 8)

You’ll need ...

Get Learning Perl on Win32 Systems now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.