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

Below you can find a standard perlscript to open a win32 application (Excel in this case) and close it again; with some actions in between. My problem:
it seems that
$excel->ActiveWorkbook->Close(0); $excel->Quit();
is not enough to 'really' close the excel.exe program. This excel.exe is only gone from the taskmanager when the perlscript itself is left. This is especially annoying when it is a perl Tk program, eg. a GUI, where after the interaction with excel, the GUI is not closed If someone has a solution to remove/kill the app in a good way, it would be most appreciated.
$excel = Win32::OLE->GetActiveObject('Excel.Application'); if ($@) { PrintError("Excel not Installed.\n"); return 0; } unless (defined $excel) { $excel = Win32::OLE->new('Excel.Application', 'Quit' ) || (sub { die "Cannot start Excel.\n";return 0 }); } $sheet = $book->Worksheets("Sheet1"); $sheet->Activate(); # some actions... $book->SaveAs($excelfile); $excel->ActiveWorkbook->Close(0); $excel->Quit();

Replies are listed 'Best First'.
Re: Really stop WIN32 OLE application
by jmcnamara (Monsignor) on Apr 04, 2004 at 22:20 UTC

    If you undef the $excel instance it should allow Excel to close:
    ... $excel->Quit(); undef $excel;

    Tested and working with WinXP and Excel2003.

    --
    John.

Re: Really stop WIN32 OLE application
by tachyon (Chancellor) on Apr 05, 2004 at 01:47 UTC

    If undef does not work for you get a sledgehammer and try Win32::KillProcess. This has yet to make CPAN but with it you can just:

    use Win32::KillProcess ':all'; $c = connectServer(); my @pids = getProcessPids( $c, 'excel.exe' ); killProcess( $c, @pids );

    Outlook is one program that seems to leave instances lurking about even when you really try to do the right thing.

    cheers

    tachyon

      just a note about the killing of processes this way.. it will kill all your instances of excel.exe if i'm reading it correctly... so you might want to try and find a method of getting the pid of the specific excel.exe that was created by your script....just a note..., undeffing it does also work.

      Grygonos

        Did I mention it was like using a sledge hammer to crack a Walnut? :-)

        cheers

        tachyon

Re: Really stop WIN32 OLE application
by Crian (Curate) on Apr 05, 2004 at 10:59 UTC
    If all else fails, put the excel-handling in a small Perl script and call it from the gui.
      *,

      I got my proces killed when doing the following:

      undef $sheet; undef $book; undef $excel;
      just undef $excel wasn't enough.
      Thx everybody for the suggestions