in reply to HowTo Delete Excel Worksheets Using WIN32::OLE

I agree with the previous responses from others. Also, davies has a very good point about the indexing of the worksheets. Another example of an indexing issue is the scenario where you're code is expecting the worksheets to be in one order, but a user rearranges the order. That will create some problems and also is the reason that I prefer to specify a worksheet by name.

As Corion pointed out, you're not giving us much information about your problem. Are you getting any errors or are you verifying afterward that the worksheet was not deleted? Also, are you saving changes upon exit? If not, your deletion is not being saved.

Just for fun, I created a blank Excel spreadsheet with multiple worksheets. I was able to use the code below to delete a worksheet.

use strict; use Win32::OLE; my $efile = "c:\\test2.xls"; my $excel_error = 0; my $excel = new Win32::OLE('Excel.Application','Quit') || ($excel_erro +r = 1); if ($excel_error) { undef $excel; die "Unable to open Excel.\n"; } my $book = $excel->Workbooks->Open($efile) || ($excel_error = 1); if ($excel_error) { undef $book; undef $excel; die "Unable to open Excel file.\n"; } $book->Worksheets('Sheet4')->Delete(); $book->Close({SaveChanges => 1}); undef $book; undef $excel;

Hopefully this might help you out.

Replies are listed 'Best First'.
Re^2: HowTo Delete Excel Worksheets Using WIN32::OLE
by sachin_p_ganjare (Initiate) on Sep 24, 2010 at 14:32 UTC

    Hello All. Thanks A lot Again To All you Monks for resolving the issue.

    Davies thanks a lot. Your pointer to excel indexing proved too good.

    Further one thing which actually resolved my problem is 'suppressing confirmation dialogues'. Sheets which are not blank, will prompt for confirmation before deleting these sheets. I suppressed this dialogue using following code & the problem was long gone.

     $Excel->Application->{DisplayAlerts}  = 0;

    And yes, 'll take care not to put titles in CAPS in future.(:-)