http://qs1969.pair.com?node_id=34310

Item Description: "Where Perl magic meets Windows"

Review Synopsis: Win32::OLE brings you the ability to control many win32 applications

Description

For win32 platforms only. Win32::OLE module gives you Automation access to Windows applications such as Word, Excel, Access, Rational Rose, Lotus Notes, and many others. Which means that your Perl scripts can harness these applications' capabilities, data and methods. Other Win32 perl modules build on this module (for example DBD::ADO, the DBI driver for Access data base).

The module lets you create perl objects that act as proxies for the application and it's components in your script. In the example below, $word is your perl proxy connected to a running instance of Word. You can call Word's Automation methods as perl methods on this object, and access the Word properties as hash elements in your perl object.

Automation-friendly Win32 applications expose hierarchies of objects and collections. The module ties these to Perl hashes and arrays for you. Your script just has to navigate the hierarchy and invoke the methods and properties as needed.

How do you know what methods and properties these objects support? Well, you RTFM that comes with the applications, and you use Object browsers that display a minimal documentation extracted from objects themselves (actually from their type libraries). The Win32::OLE module comes with Browser.html, a client-side dynamic html page. The perl code embedded in this page uses Win32::OLE to extract information from type libraries, and displays it in the html browser (IE required).

A short example will illustrate

#! perl -w use strict; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; ### open Word application and add an empty document ### (will die if Word not installed on your machine) my $word = Win32::OLE->new('Word.Application', 'Quit') or die; $word->{Visible} = 1; my $doc = $word->Documents->Add(); my $range = $doc->{Content}; ### insert some text into the document $range->{Text} = 'Hello World from Monastery.'; $range->InsertParagraphAfter(); $range->InsertAfter('Bye for now.'); ### read text from the document and print to the console my $paras = $doc->Paragraphs; foreach my $para (in $paras) { print ">> " . $para->Range->{Text}; } ### close the document and the application $doc->SaveAs(FileName => "c:\\temp\\temp.txt", FileFormat => wdFor +matDocument); $doc->Close(); $word->Quit();

Why use Win32::OLE


You work on a win32 platform and you want to tap into existing applications from your Perl scripts.
You work on creating Automation components and you want to use Perl to test them.

Why not use Win32::OLE


You don't work on a win32 platform
You do, but you prefered scripting language is VBScript (just kidding ;-)

Where is the doc, tuts and code


The module and it's html doc are included in the ActiveState Perl installation. Look up the TPJ article by Jan Dubois, the module's coauthor, for an extended example. Doc is also found on CPAN mirrors
If you want to study the module code, it is on CPAN (6700+ lines in ole.xs and 2500+ lines of perl in several packages - good reading).
For introductory tutorials, check the ppt presentation and demos ( Word, Excel)
Not enough ? You can find more short tutorials down under ...

Replies are listed 'Best First'.
RE (tilly) 1: Win32::OLE
by tilly (Archbishop) on Sep 28, 2000 at 04:46 UTC
    Your die if Word is not accessible should be more informative. At the least it should use Win32::OLE->LastError(). Error handling and how you use the module's methods to come up with good messages is one of the first things you should look for.
      I tried runnin this code and received a "constant subroutine emptyenum redefined at C:/Perl/site/lib/Win32/OLE/Cons.pm line-1" error.. what does this mean?
        It means that somehow "emptyenum" got defined twice. You can search the code for that string and see if you can figure it out.

        I suspect that there is either something unusual about your usage, or you have a messed up installation. But I don't know as I no longer use Windows.

        If you can't figure it out on your own, you could post to Seekers of Perl Wisdom and see if anyone else has seen this error. (Replies in buried threads are unlikely to be seen and responded to...)

Re: Win32::OLE-Browser
by sekitan (Beadle) on Mar 15, 2005 at 20:05 UTC
    I would very much like to learn how to better use the OLE-Browser packaged with activestate perl. Is there a good tutorial or detailed documentation anywhere?
      Hi sekitan

      I don't know of any tutorial on OLE-Browser. I would summarize what I know about it as follows:

      You start it by opening the file C:\Perl\html\OLE-Browser\Browser.html in MS IE (it won't work in other browsers, AFAIK).

      If you click on IE menu option 'Allow blocked content', the browser will scan the Windows Registry and show you a list of Type Libraries, so you can browse the Interfaces and their methods and properties.

      How would you use this information? Well, this is a MS COM/Automation question, not a perl question.

      HTH

      Rudif