Linux

Wednesday, April 22, 2009

Excel macro from Perl

A Macro defined in Excel can be called from Perl. Here is the following code derived on googling that did the magic

use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3;

my $Excel = Win32::OLE->GetActiveObject('Excel.Application')||Win32::OLE->new('Excel.Application', 'Quit'); # use the Excel application i
#f it's open, otherwise open new

my $Book = $Excel->Workbooks->Open( "c:\\Test.xls" ); # open the file

my $Sheet = $Book->Worksheets(2);
printf ("the value is %s \n",$Sheet->Cells(1,3)->{'Value'});

$Excel->Run("SetIO");
$Excel->Run("CommandButton1_Click");
#$Excel->Run("Temp");
$Book->Save; #optional - save any changes made by the macro
$Book->Close;

Labels: