473,396 Members | 2,039 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Create another Excel instance

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_MULTITHR EADED)
import win32com.client.dynamic
self.xlApp = win32com.client.dynamic.Dispatch("Excel.Applicatio n")
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
Jul 18 '05 #1
3 4293
"yvan" <yv**************@hotmail.com> wrote in message
news:9e**************************@posting.google.c om...
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_MULTITHR EADED)
import win32com.client.dynamic
self.xlApp = win32com.client.dynamic.Dispatch("Excel.Applicatio n")
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/
Jul 18 '05 #2
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message news:<FZ******************@twister.nyroc.rr.com>.. .
"yvan" <yv**************@hotmail.com> wrote in message
news:9e**************************@posting.google.c om...
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_MULTITHR EADED)
import win32com.client.dynamic
self.xlApp = win32com.client.dynamic.Dispatch("Excel.Applicatio n")
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.


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?
Jul 18 '05 #3
yv**************@hotmail.com (yvan) wrote in message news:<9e**************************@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/pytho...pter/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
---------------------------------------------------------------
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rick Brown | last post by:
After reading 30+ threads on the subject and implementing pertinent changes I still have an instance of Excel that won't close. I hope its due to my poor coding and someone can spot the error....
6
by: Mark | last post by:
I was able to get procedure to work in a VB.Net Windows application, and want to get it to work within a ASP.Net page. It won't create the instance of Excel. It blows up on the CreateObject...
1
by: Vlad | last post by:
I am trying to decide whether I should buy Visual Basic.NET Standard. I’d like to know the answer to the following questions to help me decide 1. Can I use Visual Basic.NET Standard to create a...
0
by: Thanks | last post by:
I would like to output a dataset to excel from a text file but did not work. I alreay add the microsoft active component 2.7 and 2.8 and microsoft excel 11 component library but no luck. Please...
3
by: Ian Dunn | last post by:
I'm simply trying to access an instance of Excel that has been opened manually by the user in order to put a few values in the existing sheet. Here's the code I've tried: Dim oXL As...
7
by: jwang | last post by:
I am trying to instantiate a COM component in C# via late binding. The component appears to be launched, then it automatically gets unloaded. That is, the instance is unloaded after the...
16
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public...
13
by: miztaken | last post by:
Hi, My C# application have a following code to create an instance of Visual Studio. System.Type type = System.Type.GetTypeFromProgID("VisualStudio.DTE. 8.0"); Object obj =...
24
rauty
by: rauty | last post by:
Hi all, I'm trying to access the most recent instance of Excel so I can paste data. What works for me is if Excel isn't already opened, I open it and can paste the data where I want to. What...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.