ActivePerl FAQ - Using OLE With Perl
ActivePerl FAQ - Using OLE With Perl
http://docs.activestate.com/activeperl/5.6/faq/Windows/ActivePerl-Wi...
Cart
Table of Contents
NAME
ActivePerl-faq12 - Using OLE with Perl
DESCRIPTION
How to use OLE automation with Perl - through the Win32::OLE module
1 of 6
27-02-14 17:32
http://docs.activestate.com/activeperl/5.6/faq/Windows/ActivePerl-Wi...
using the new Win32::OLE module options. Look at the Win::OLE module documentation (under Incompatibilities) and in particular the 'What's changed from 300 series builds' in the release notes.
or simply
$Word->ActiveDocument->PrintOut;
Now $array[0][0] contains the value of cell A8, $array[0][1] the value of cell B8, $array[1][0] the value of cell A9 and $array[1][1] the value of cell B9. What is returned is an two-dimensional array (OK, an array with references to arrays) that contains the values of the requested cells. A complete example is here:
use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn = 3; # die on errors... my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); # get already active Excel # application or open new my $Book = $Excel->Workbooks->Open("C:\\DOCUMENTS\\test.xls"); # open Excel file my $Sheet = $Book->Worksheets(1); # select worksheet number 1 my $array = $Sheet->Range("A8:B9")->{'Value'}; # get the contents $Book->Close; foreach my $ref_array (@$array) { # loop through the array # referenced by $array foreach my $scalar (@$ref_array) { print "$scalar\t"; } print "\n"; }
To retrieve the formatted value of a cell you should use the {'Text'} property instead of the {'Value'} property. This returns exactly what is being displayed on the screen though! If the column is not wide enough, you get a value of '######':
my $array = $Sheet->Range("A8:B9")->{'Text'};
[x]
2 of 6
27-02-14 17:32
http://docs.activestate.com/activeperl/5.6/faq/Windows/ActivePerl-Wi...
use Win32::OLE; use Win32::OLE::Const 'Microsoft Excel'; my $Excel = Win32::OLE->new("Excel.Application"); $Excel->{Visible} = 1; my $Book = $Excel->Workbooks->Add; my $Sheet = $Book->Worksheets(1); my $Range = $Sheet->Range("A2:C7"); $Range->{Value} = [['Delivered', 'En route', 'To be shipped'], [504, 102, 86], [670, 150, 174], [891, 261, 201], [1274, 471, 321], [1563, 536, 241]]; my $Chart = $Excel->Charts->Add; $Chart->{ChartType} = xlAreaStacked; $Chart->SetSourceData({Source => $Range, PlotBy => xlColumns}); $Chart->{HasTitle} = 1; $Chart->ChartTitle->{Text} = "Items delivered, en route and to be shipped";
A complete example that opens an Excel workbook, loops through all the charts and saves them as GIFs and then closes the Excel workbook is here:
use strict; use Win32::OLE qw(in with); use Win32::OLE::Const; use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn = 3; # die on errors... my $filename = 'c:\\documents\\test.xls'; my $filter = 'GIF'; # can be GIF, JPG, JPEG or PNG my $count = 0; my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); # use the Excel application if it's open, otherwise open new my $Book = $Excel->Workbooks->Open( $filename ); # open the file foreach my $Sheet (in $Book->Sheets) { # loop through all sheets foreach my $ChartObj (in $Sheet->ChartObjects) { # loop through all chartobjects in the sheet my $savename = "$filename." . $count++ . ".$filter"; $ChartObj->Chart->Export({ FileName => $savename, FilterName => $filter, Interactive => 0}); } } $Book->Close;
In order to do this, you of course need to have a macro in Excel that's called 'PrintPDFFile'...
[x]
3 of 6
27-02-14 17:32
http://docs.activestate.com/activeperl/5.6/faq/Windows/ActivePerl-Wi...
# adds a record
print "This didn't go well: ", Win32::OLE->LastError(), "\n"; if (Win32::OLE->LastError()); $RS->Close; $Conn->Close;
To get further than this you should have a look at the ADO FAQ at http://www.fastnetltd.ndirect.co.uk /Perl/perl-win32-database.html or Jan Dubois article in TPJ#10 (visit The Perl Journal at http://tpj.com/).
You can access all objects that are accessible to LotusScript, and the LotusScript classes can be seen at http://www.lotus.com/products/lotusscript.nsf. A good idea would also be to read Jan Dubois article in TPJ#10 (visit The Perl Journal at http://tpj.com/)
[x]
4 of 6
27-02-14 17:32
http://docs.activestate.com/activeperl/5.6/faq/Windows/ActivePerl-Wi...
object.method(argument).property = value
OK, but can I at least find the constants that are exported from Win32::OLE::Const?
Yes, you can use the following code example to view all the constants - you really shouldn't need this, but if you want to know what's going on, it might help:
use strict; use Win32::OLE; use Win32::OLE::Const; my $xl = Win32::OLE::Const->Load("Microsoft Excel"); printf "Excel type library contains %d constants:\n", scalar keys %$xl; foreach my $Key (sort keys %$xl) { print "$Key = $xl->{$Key}\n"; }
[x]
5 of 6
27-02-14 17:32
http://docs.activestate.com/activeperl/5.6/faq/Windows/ActivePerl-Wi...
to use. In the case of Microsoft Office 97, make sure that you have at least updated to Service Release 1 - much of the OLE in Microsoft Office 97 is broken without this update.
2012 ActiveState Software Inc. All rights reserved. ActiveState, Komodo, ActivePerl, ActivePython, ActiveTcl, and Stackato are registered trademarks of ActiveState. All other marks are property of their respective owners.
[x]
6 of 6
27-02-14 17:32