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";
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|