johntio has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I would like to ask for help regarding openning the excel file. There seems to be errror that i could not debug it. It can sometimes run succesfull and sometimes show error during openninng the sheet Below is the code that i have written:
use strict; use Win32::OLE qw(in with); use Win32::OLE::Const; use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn = 3; # die on errors... my $filename = 'X:\KFD_Plot_Data.xls'; my $filename1 = 'X:\KFD_Raw_Data.csv'; my $filter = 'GIF'; # can be GIF, JPG, JPEG or PNG my $count = 0; my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); # use the Excel +application if it's open, otherwise open new #my $Excel = Win32::OLE->new('Excel.Application', 'Quit'); # use the +Excel application if it's open, otherwise open new my $Book1 = $Excel->Workbooks->Open( $filename1 ); # open the fil +e my $Book = $Excel->Workbooks->Open( $filename ); # open the file #$Book1->Close; foreach my $Sheet (in $Book->Sheets) { # loop through a +ll sheets print "Start $Sheet \n"; foreach my $ChartObj (in $Sheet->ChartObjects) { # loop through a +ll chartobjects in the sheet my $savename = "$filename." . $count++ . ".$filter"; $ChartObj->Chart->Export({ FileName => $savename, FilterName => $filter, Interactive => 0}); print "Done Saving $savename \n"; } } #my $Sheet1 = $Book->Sheets(3); #my $ChartObj = $Sheet1->ChartObjects; #my $savename1 = "Yield". ".$filter"; # $ChartObj->Chart->Export({ # FileName => $savename1, # FilterName => $filter}); #$Book->{Saved} = 1; $Book->SaveAs( 'KTF_unused.xls' ); $Book->Close; #$Book1->Close; #$Excel->Quit();
Hope that you can help me. Thanks in advance.

2006-02-09 Retitled by planetscape, as per Monastery guidelines
Original title: 'Error in the code'

Replies are listed 'Best First'.
Re: Problem opening Excel file using Win32::OLE
by Util (Priest) on Feb 09, 2006 at 03:47 UTC

    Perhaps one of your files is already open, but you can't see it due to Excel being invisible. Try adding this line just after GetActiveObject()||new():

    $Excel->{'Visible'} = 1; # if you want to see what's going on

    Even if this solves your problem, please post the original error message as McDarren requested; it will help other Monks find this thread in the future.

Re: Problem opening Excel file using Win32::OLE
by McDarren (Abbot) on Feb 09, 2006 at 02:53 UTC
    sometimes run succesfull and sometimes show error
    And the error you get is.....?
Re: Problem opening Excel file using Win32::OLE
by johntio (Initiate) on Feb 09, 2006 at 05:47 UTC
    Below is the error i get from running the script. This excel file i would like to open it invisible so that i can run it on the background.
    Win32::OLE(0.1603) error 0x8001010a: "The message filter indicated tha +t the application is busy" in METHOD/PROPERTYGET "" at C:\graph\test1.pl line 18
      The following line of code appears to be superfluous:

      #my $Excel = Win32::OLE->new('Excel.Application', 'Quit'); # use the +Excel application if it's open, otherwise open new


      why not delete it...

      Also, try using the following code which I grabbed from the ActivePerl User Guide to check to see if Excel is already running(obviously replacing with your filename variables):

      # use existing instance if Excel is already running eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')}; die "Excel not installed" if $@; unless (defined $ex) { $ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Qui +t;}) or die "Oops, cannot start Excel"; }


      Scott