Create another Excel instance
Cy Edmunds
cedmunds at spamless.rochester.rr.com
Thu Jul 10 19:51:01 EDT 2003
"yvan" <yvan_charpentier at hotmail.com> wrote in message
news:9ee55987.0307101339.701965a7 at posting.google.com...
> 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
I haven't tried this myself but maybe...
if self.xlApp != None:
nbook = self.xlApp.Workbooks.Count # number of open workbooks
if nbook == 0:
self.xlApp.Quit()
Let us know how you made out.
--
Cy
http://home.rochester.rr.com/cyhome/
More information about the Python-list
mailing list