Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > closing excel application

Reply
Thread Tools

closing excel application

 
 
federico
Guest
Posts: n/a
 
      10-17-2003
When I close an excel application opened from a python program with .Close()
methood , in tasks manager is still present 'excel.exe' process and if I
open manually an excel applicaion I have a transparent excel window... my os
is win 2000 pro... Which is the right way to close an excel applicatio from
python?
Thanks


 
Reply With Quote
 
 
 
 
John J. Lee
Guest
Posts: n/a
 
      10-18-2003
"federico" <(E-Mail Removed)> writes:

> When I close an excel application opened from a python program with .Close()
> methood , in tasks manager is still present 'excel.exe' process and if I

[...]

There was a thread here about this just two weeks ago. Google
(Groups) for it.


John
 
Reply With Quote
 
 
 
 
Hung Jung Lu
Guest
Posts: n/a
 
      10-18-2003
"federico" <(E-Mail Removed)> wrote in message news:<bmqrof$feo$(E-Mail Removed)>...
> When I close an excel application opened from a python program with .Close()
> methood , in tasks manager is still present 'excel.exe' process and if I
> open manually an excel applicaion I have a transparent excel window... my os
> is win 2000 pro... Which is the right way to close an excel applicatio from
> python?
> Thanks


The "right way" would be:

import win32com.client
import pythoncom
app = win32com.client.Dispatch('Excel.Application')
app.Visible = 1
....
print pythoncom._GetInterfaceCount()
app = None
print pythoncom._GetInterfaceCount()
pythoncom.CoUninitialize()

Pythoncom holds and manages the reference count. Which, you cannot
tweak with. (At least that's the intention of Mark Hammond... you
don't have access to the Release() method of the IUnknown interface.)
Therefore, ideally, you should catch all exceptions in your program,
and exit your Python program gracefully and invoke the
CoUninitialize() method to release all references to COM objects. That
is, your program is should not fail due to unhandled exceptions...
otherwise you will have dangling reference to COM objects, which you
cannot recover in new sessions of Python.

But the world is never ideal.

Here are a few "solutions". I hope others can contribute more.

(1) Simply kill the dangling Excel by hand from the task manager.
(2) Really, really, really make sure you use exception handling in
your program, and always exit your program gracefully by invoking
pythoncom.CoUnintialize().
(3) Find out the process id, and kill the dangling process before
anything else. The windows APIs provided by Mark Hammand may allow
this, but I have used instead the tlist.exe and kill.exe executables
from outside Python (you can launch them using os.system() or some
other ways of executing system command. I remember having problems
with Windows 98, though, some piping issues, but after adding one more
little file it was solved.)
(4) Write yourself a little COM program in C/C++ to invoke the
Release() method of the IUnknown() interface. You might even be able
to do this from using Thomas Heller's ctypes module without actually
programming in C/C++. I never tried it, though.

Of course the "right way" is alternative (2). I'd like to hear other
people's solutions.

--------------------

The Dispatch() late binding method is deceivingly easy to use, but
once you start to use it, you will realize that there are a lot of
issues. Using COM programming this way is best limited to simple,
routine tasks. Of course, that's just my opinion.

Hung Jung
 
Reply With Quote
 
Hung Jung Lu
Guest
Posts: n/a
 
      10-20-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Hung Jung Lu) wrote in message
>
> import win32com.client
> import pythoncom
> app = win32com.client.Dispatch('Excel.Application')
> app.Visible = 1
> ...
> print pythoncom._GetInterfaceCount()
> app = None
> print pythoncom._GetInterfaceCount()
> pythoncom.CoUninitialize()


Sorry, missed the line app.Quit() before the first print statement.

import win32com.client
import pythoncom
app = win32com.client.Dispatch('Excel.Application')
app.Visible = 1
....
app.Quit()
print pythoncom._GetInterfaceCount()
app = None
print pythoncom._GetInterfaceCount()
pythoncom.CoUninitialize()

Hung Jung
 
Reply With Quote
 
federico
Guest
Posts: n/a
 
      10-20-2003
Thanks a lot for your answer

"Hung Jung Lu" <(E-Mail Removed)> ha scritto nel messaggio
news:(E-Mail Removed) m...
> (E-Mail Removed) (Hung Jung Lu) wrote in message
> >
> > import win32com.client
> > import pythoncom
> > app = win32com.client.Dispatch('Excel.Application')
> > app.Visible = 1
> > ...
> > print pythoncom._GetInterfaceCount()
> > app = None
> > print pythoncom._GetInterfaceCount()
> > pythoncom.CoUninitialize()

>
> Sorry, missed the line app.Quit() before the first print statement.
>
> import win32com.client
> import pythoncom
> app = win32com.client.Dispatch('Excel.Application')
> app.Visible = 1
> ...
> app.Quit()
> print pythoncom._GetInterfaceCount()
> app = None
> print pythoncom._GetInterfaceCount()
> pythoncom.CoUninitialize()
>
> Hung Jung



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Excel reports ::::Excel 2003 Migration To Excel 2007 =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?= ASP .Net 15 10-24-2007 01:34 PM
Closing popup window when closing parent window? =?Utf-8?B?Vk1J?= ASP .Net 3 02-15-2007 08:29 AM
Closing the doors 15 minutes before closing. doofus Computer Support 12 06-11-2005 08:20 AM
closing excel Mark ASP .Net 2 04-14-2004 07:56 AM
Closing child window WITHOUT closing parent thomas Javascript 0 10-23-2003 04:10 PM



Advertisments