[python-win32] Closing COM object (e.g. Excel) when Python app dies

Warren Sande wsande at tropicnetworks.com
Thu Mar 2 15:06:37 CET 2006


 have a PythonCard app that opens up an Excel spreadsheet using the
win32com module. 
 
I started doing this in Perl, then recently switched to Python (and
PythonCard), which I am just learning.
 
In Perl, I found the following code snippet which I used in my app:
 
----------------------
# 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]->Quit;})
             or die "Oops, cannot start Excel";
     $ex->{Visible} = 1;
 }  # Note the destructor specified on the Win32::OLE->new method. 
    # It ensures that Excel will shutdown properly even if the Perl
program dies.
----------------------
 
I would like to be able to do the same in Python, but don't know if it
is supported, and can't find or figure out the syntax if it is.
 
I know this is not really a PythonCard question, but I'm hoping that
other PythonCard users are working with win32com and might be able to
help me out.
 
My code to open the excel object is simply this:
 
----------------------
import win32com as wc
...
 
self.xl = wc.Dispatch("Excel.Application")
self.xl.Visible = 1
...
 
self.xl.Quit()
----------------------
 
Warren Sande
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-win32/attachments/20060302/b6e14725/attachment.html 


More information about the Python-win32 mailing list