Python and Excel

Hans Kristian Ruud hans.kristian.ruud at inenco.no
Fri Oct 27 06:07:00 EDT 2000


"Howard B. Golden" wrote:

> "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

Thank you both for your advice. After experimenting, I find that Mark Hammonds
advise
(zapping xlBook as well as xlApp) solved my problem.

--
Hans Kristian





More information about the Python-list mailing list