473,434 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 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 20924
"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 combinations of them ;-). It still doesn't work. I'll...
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 my application, there is an instance of Excel in...
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 passing the excel reference to the one of forms...
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 to the spreadsheet to ensure that that the data...
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 instance of Excel still remains in memory. I...
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 instance of excel is not removed from task manager. ...
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 I've been learning javascript as I go. A problem...
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 savepath; bool foundPID; int ourPID = 0; int...
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 garbage collector to dispose excel application object,...
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 excel file and grab some data. The problem I am...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.