472,334 Members | 2,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,334 software developers and data experts.

closing excel application

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
Jul 18 '05 #1
4 20370
"federico" <ma********@hotmail.com> 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
Jul 18 '05 #2
"federico" <ma********@hotmail.com> wrote in message news:<bm**********@lacerta.tiscalinet.it>...
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
Jul 18 '05 #3
hu********@yahoo.com (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
Jul 18 '05 #4
Thanks a lot for your answer

"Hung Jung Lu" <hu********@yahoo.com> ha scritto nel messaggio
news:8e*************************@posting.google.co m...
hu********@yahoo.com (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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mark | last post by:
Hi all, as many had, i have some problems terminating the excel-process. I implemented the solutions i found in this forums. Tried most...
2
by: Atley | last post by:
I have written an application that exports data from SQL to Excel. It all works perfectly except that if you open the Task Manager after running...
0
by: Sania | last post by:
Hi, We have an application that create an excel object on the very beginning of the application: objExcel = New Excel.Application Then I am...
7
by: rdemyan via AccessMonster.com | last post by:
I want to make sure that I'm closing an opened spreadsheet correctly. I've been having some locking up problems. The following code adds a dummy row...
1
by: John Bailo | last post by:
I wrote a c# web service that creates an Excel spreadsheet. Even though I follow all the formal procedures for closing the app and finalizing, an...
6
by: pleaseexplaintome_2 | last post by:
Help please. The excel instance is removed from task manager when I run the code below. If I uncomment the lines pertaining to a workbook, the...
4
by: Yoduh | last post by:
Hello, I'm fairly new to javascript. I'm making a website for my personal use that will read from Excel and display some data on an HTML page and...
1
by: fakehitswizard | last post by:
this is the correct way to close excel with C#. I've seen alot of other bogus posts ALL over the web that don't work, how frustrating. string...
2
by: Ronin85 | last post by:
Hi , I recently have much pain working with excel application especially closing excel . I try several method but with no success i try to urge...
2
by: Silgd1 | last post by:
Hi All.... I'm using python 2.4, Win XP Pro v.2002 sp3, and I use pyscripter 1.9.9.2 as my editor. I have written code to open an existing...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.