[python-win32] Error Quitting Excel Instance when One is Already Open

Mark Mordeca mark.mordeca at prosensus.ca
Tue May 24 21:08:21 CEST 2011


Greetings,



I am having a problem with using win32com to open an instance of Excel when
there is already one open.   I would appreciate any help.



My example goes as follows:

The user has an Excel file (and therefore an EXCEL.exe process) open
already.



I do:

xl=win32com.client.DispatchEx("Excel.Application")   #create a new instance
of EXCEL.exe that will not interfere with the already open process.  It
remains hidden.

book=xl.Workbooks.Open(filename)                       #open the excel file



There are now 2 EXCEL.exe’s in the Task Manager.



After this, but before I am done with the Excel file I opened with
DispatchEx, the user then closes their open Excel file.  There is now only
one EXCEL.exe in the task manager, supposedly the one that I opened with
DispatchEx.



I then call:

try:

   book.Close()



and receive the error:

Exception pywintypes.com_error: com_error(-2147418111, 'Call was rejected by
callee.', None, None)



I then go into an except statement and do:

nbooks=xl.Workbooks.Count        <--FAILS HERE

if not xl.Visible and nbooks==0:

                # if excel is not visible and there are no books open, close
excel.

                # Do this because another program may have excel open in the
background.

                print "Asking Excel to Quit"

                xl.Quit()

xl=None



At “FAILS HERE” I get the same error again: “Exception pywintypes.com_error:
com_error(-2147418111, 'Call was rejected by callee.', None, None)” and the
EXCEL.exe is left hanging in the process manager, which I have to now
manually kill.



Can anyone help me with this error and how to keep connected to the Excel
instance I opened with DispatchEx no matter what the user does with their
own open Excel which includes the user closing it?



Thank you for your assistance.

Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20110524/2edd2644/attachment.html>


More information about the python-win32 mailing list