AM,
Firstly please but < code> # code in here </code> next time you post a code snippet, It makes it alot easier to read.
In the past i've had similar problems when deleteing worksheets the best way i've found is to ensure Alerts are turned of every time you try and deleted something.
Small example here of how you'd add a Book & Sheet, Delete the sheet, then close the workbook:
#!usr/bin/perl -w
use strict;
use Win32::OLE qw( HRESULT );
use constant False => 0;
use constant True => 1;
my $Excel;
{
# Open Excel
$Excel = Win32::OLE->new( 'Excel.Application', sub { $_[0]->Quit;
+} )
|| die( '[FATAL]: could not open Excel', $! );
$Excel->{Visible} = False;
}
{
my $Book = $Excel->Workbooks->Add; # Add workbook
my $Sheet = $Book->Worksheets->Add; # Add worksheet to work boo
+k
$Excel->{DisplayAlerts} = False; # Don't display alerts
$Sheet->Delete(); # Delete sheet
$Book->Close( { SaveChanges => False } ); # Close the work book
# Excel exits via the deconstructor closure sub { $_[0]->Quit }; liste
+d in the open method
}
Your problem could be as simple as calling
$Excel->{DisplayAlerts} = False everytime.
As far as i know catching errors from Ole is tricky. If you see when i load Win32::Ole i also import HRESULT. This method allows you to compare the last error message generated to one you might be expected.eg.
if( Win32::Ole->LastError == HRESULT(0x800a01a8)){
print "Error\n";
}
use warnings; or
-w
Only warns you about coding errors you've made have a look at the
perlexwarn for more info. If you're getting an error from Excel or Word then it's external to perl. As you may or maynot be aware the ole module loads the libraries to control the MS office either by DynaLoader or XS, I'm sure on the specifics the point is they're external to perl and therefore it is hard to catch errors.
Hope that helps
John
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.