Create another Excel instance

David Rushby woodsplitter at rocketmail.com
Fri Jul 18 09:00:16 EDT 2003


yvan_charpentier at hotmail.com (yvan) wrote in message news:<9ee55987.0307111159.6978676e at posting.google.com>...

> Basically, my problem is that the object uses the same process 
> if an instance of Excel already exists.
> How can i create the new instance in a different process?

I just encountered the same problem.  How about this as a solution?
---------------------------------------------------------------
# For an explanation of makepy and gencache, see:
# http://www.oreilly.com/catalog/pythonwin32/chapter/ch12.html
# (Chapter 12 of _Python Programming on Win32_)
from win32com.client import gencache
gencache.EnsureModule('{00020813-0000-0000-C000-000000000046}', 0, 1, 2)
xlApplicationClass = gencache.GetClassForProgID('Excel.Application')

# Create a new instance of the Excel.Application class--don't reuse an
# existing instance.
xlApp = xlApplicationClass()

xlApp.Visible = False
xlApp.DisplayAlerts = False

try:
    wbk = xlApp.Workbooks.Add()
    sht = wbk.Sheets(1)

    sht.Cells(1,1).Value = 'blah'
    raise Exception('Deliberately raise an exc to test tidiness of cleanup.')
finally:
    try:
        wbk.Close()
        del wbk
    except:
        pass

    xlApp.Quit()
    del xlApp
---------------------------------------------------------------




More information about the Python-list mailing list