Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??


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 ...

In reply to Win32::OLE by Rudif

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.