Create another Excel instance

yvan yvan_charpentier at hotmail.com
Thu Jul 10 23:39:35 CEST 2003


I am using Excel to save data.
Everything works as i intend it to if no other instance of Excel is running.
If another instance is running, it will do the job, but also close that instance.
How can i prevent that from happening?

Here is the code that creates and deletes the instance:
class CExcel:
    def __init__(self, bVisible = 0):
        import sys
        import pythoncom
        sys.coinit_flags = 0
        pythoncom.CoInitializeEx(pythoncom.COINIT_MULTITHREADED)
        import win32com.client.dynamic
        self.xlApp = win32com.client.dynamic.Dispatch("Excel.Application")
        self.xlApp.Visible = bVisible
        self.xlBook = self.xlApp.Workbooks.Add()
        self.xlSheet = self.xlApp.ActiveSheet

    def __del__(self):
        import pythoncom
        if self.xlSheet != None:
            del(self.xlSheet)
        if self.xlBook != None:
            self.xlBook.Close(0)
            del(self.xlBook)
        if self.xlApp != None:
            self.xlApp.Quit()
            del(self.xlApp)
        pythoncom.CoUninitialize()

Thank for your help,

-Yvan




More information about the Python-list mailing list