Sadly this is all under the control of Excel, so I've no good advice. The poor advice I can offer is (1) for your own piece of mind, see if you can get the same basic behaviour with wscript and a .vbs script, (2) to try and see if you can find a similar problem in any language/environment (eg, wscript) and how they worked around it and finally (3) see if you can worm around it by catching that exception then discarding the object you have and re-connecting to that instance using normal Dispatch().

HTH,

Mark

On 25/05/2011 5:08 AM, Mark Mordeca wrote:
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



_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

_______________________________________________
python-win32 mailing list
python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32

Reply via email to