Create another Excel instance

Y

yvan

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
 
C

Cy Edmunds

yvan said:
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.
 
Y

yvan

Cy Edmunds said:
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.

It looks like that would work if 'Visible' was set to 1. Unfortunately
mine has to be 0. Thanks for the suggestion.
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?
 
D

David Rushby

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
---------------------------------------------------------------
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top