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"; }

In reply to Win32::OLE Excel Adding worksheet after last current sheet by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.