closing excel application

Discussion in 'Python' started by federico, Oct 17, 2003.

  1. federico

    federico Guest

    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
    federico, Oct 17, 2003
    #1
    1. Advertising

  2. federico

    John J. Lee Guest

    "federico" <> 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
    John J. Lee, Oct 18, 2003
    #2
    1. Advertising

  3. federico

    Hung Jung Lu Guest

    "federico" <> wrote in message news:<bmqrof$feo$>...
    > 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
    Hung Jung Lu, Oct 18, 2003
    #3
  4. federico

    Hung Jung Lu Guest

    (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
    Hung Jung Lu, Oct 20, 2003
    #4
  5. federico

    federico Guest

    Thanks a lot for your answer

    "Hung Jung Lu" <> ha scritto nel messaggio
    news:...
    > (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
    federico, Oct 20, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark

    closing excel

    Mark, Apr 13, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,992
  2. Winshent
    Replies:
    0
    Views:
    1,947
    Winshent
    Nov 3, 2004
  3. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    648
    Thomas Hansen
    Feb 15, 2007
  4. =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=

    Problem with Excel reports ::::Excel 2003 Migration To Excel 2007

    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=, Oct 5, 2007, in forum: ASP .Net
    Replies:
    15
    Views:
    1,494
    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=
    Oct 24, 2007
  5. thomas
    Replies:
    0
    Views:
    224
    thomas
    Oct 23, 2003
Loading...

Share This Page