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 to check the documentation for the automation server that you want to use in order to discover what its ProgID is.
CreateObject returns a reference to the automation object if it succeeds, and undef
if it fails.
The libwin32 version of CreateObject
uses Win32::OLE
as the module name (this was done for conformity with the other Win32 extensions). CreateObject
takes the same ProgID, and a scalar that will contain the automation object if the function returns successfully.
Throughout this chapter, we'll be using the ActiveState syntax for our automation examples.