I don't want to use the writeExcel module. I need to use the OLE methods. I'm trying to add a new worksheet after the last worksheet. It's not working and I don't know why. I thought the following code would work:
my $Sheet2 = $Book->Worksheets->Add({after}=>$Book->Worksheets($Book->Worksheets->{count}));
With use strict I get the error message:
Bareword "after" not allowed while "strict subs" in use
Without it it still doesn't work. I get the error message:
Unable to get the Add property of the Sheets class
Win32::OLE(0.1603) error 0x800a03ec
in METHOD/PROPERTYGET "Add" at
Any tips? Here is a complete example.
#!e:/perl/bin/perl.exe -w
use strict;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';
if(scalar @ARGV != 1) {
usage();
exit 1;
}
$Win32::OLE::Warn = 3; # die on errors...
# get already active Excel application or open new
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
|| Win32::OLE->new('Excel.Application', 'Quit') or die Win32::OLE
+->LastError();
$Excel->{'Visible'} = 1; # 1 is visible, 0 is not visible
my $path = $ARGV[0];
my $filename = Win32::GetFullPathName($path);
my $Book = $Excel->Workbooks->Open($filename) or die Win32::OLE->LastE
+rror();
###############
# Adding new worksheet
##############
#my $Sheet2=$Book->WorkSheets->Add; #This works ok
my $Sheet2 = $Book->Worksheets->Add({after}=>$Book->Worksheets($Book->
+Worksheets->{count})); # Does not work
#my $Sheet2=$Book->WorkSheets->Add;
my $name = $Sheet2->Name;
print "Working on sheet name: $name\n";
#$Sheet2->Move(After=> $Book->Worksheets->Count); # Does not work
$Sheet2 -> Activate();
$Sheet2->{Name} = 'CSV';
$name = $Sheet2->Name;
print "Working on sheet name: $name\n";
# open Excel file
my $wkSheetCount = $Book->Worksheets->Count;
foreach my $sheetnum (1..$wkSheetCount)
{
my $Sheet = $Book->Worksheets($sheetnum);
$Sheet -> Activate();
$name = $Sheet->Name;
print "Working on sheet # $sheetnum - Its name is $name\n";
}
$Book->Close;
undef $Book;
$Excel->Quit;
exit 1;
######################################################################
+#########
# usage
#
######################################################################
+#########
sub usage{
print "Usage:\n" .
"\tperl $0 Test.xls \n";
}