Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Win32::OLE Excel Adding worksheet after last current sheet
by jsprat (Curate) on Nov 05, 2003 at 20:35 UTC | |
by Anonymous Monk on Nov 05, 2003 at 21:30 UTC | |
by Anonymous Monk on Apr 30, 2009 at 10:14 UTC | |
by Corion (Patriarch) on Apr 30, 2009 at 10:26 UTC | |
by dipak28 (Initiate) on Apr 30, 2009 at 10:25 UTC | |
Re: Win32::OLE Excel Adding worksheet after last current sheet
by Nkuvu (Priest) on Nov 05, 2003 at 19:29 UTC | |
by Anonymous Monk on Nov 05, 2003 at 19:47 UTC |
Back to
Seekers of Perl Wisdom