Python and Excel

Howard B. Golden howardbgolden at earthlink.net
Thu Oct 26 21:09:53 EDT 2000


"Mark Hammond" <MarkH at ActiveState.com> wrote:
> "Hans Kristian Ruud" <hans.kristian.ruud at inenco.no> wrote:
>
> > When the application is finished, it tries to shut Excel down by
> calling
> > xlBook.Close():
> >
> > xlBook.Close(SaveChanges=0)
> > del xlApp
>
> Note that you are zapping xlApp, but _not_ xlBook.  This object still
> holds a reference to Excel.  Try deleting this too.
>
> Also, you can try calling "pythoncom.CoUninitialize()" as you
> terminate.  This _may_ prevent the crash.
>

In my experience automating Excel 97 from VBA, crashes are common.  I
believe that there are race conditions between the threads of Excel.  I have
generally solved the problem in VBA by adding calls to VBA's "DoEvents" as
necessary.  In fact, I routinely call a subroutine that executes a loop of
50(!) DoEvents whenever necessary.

I suspect you may benefit from the Python equivalent of this, which Mark
described to me as:

    pythoncom.PumpWaitingMessages()

You would probably want to do this several times (though maybe not 50) each
time it is necessary.

HTH,

Howard





More information about the Python-list mailing list