Create another Excel instance

Discussion in 'Python' started by yvan, Jul 10, 2003.

  1. yvan

    yvan Guest

    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
     
    yvan, Jul 10, 2003
    #1
    1. Advertising

  2. yvan

    Cy Edmunds Guest

    "yvan" <> wrote in message
    news:...
    > 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/
     
    Cy Edmunds, Jul 11, 2003
    #2
    1. Advertising

  3. yvan

    yvan Guest

    "Cy Edmunds" <> wrote in message news:<FZmPa.28943$>...
    > "yvan" <> wrote in message
    > news:...
    > > 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.


    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?
     
    yvan, Jul 11, 2003
    #3
  4. yvan

    David Rushby Guest

    (yvan) wrote in message news:<>...

    > 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
    ---------------------------------------------------------------
     
    David Rushby, Jul 18, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sandra-24
    Replies:
    18
    Views:
    1,919
    Steve Holden
    Apr 29, 2006
  2. =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=

    Problem with Excel reports ::::Excel 2003 Migration To Excel 2007

    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=, Oct 5, 2007, in forum: ASP .Net
    Replies:
    15
    Views:
    1,572
    =?Utf-8?B?c2hhc2hhbmsga3Vsa2Fybmk=?=
    Oct 24, 2007
  3. Martin P. Hellwig
    Replies:
    1
    Views:
    383
    Martin P. Hellwig
    Mar 26, 2010
  4. Q. John Chen

    Render to Excel - only one excel instance

    Q. John Chen, Aug 28, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    217
    Q. John Chen
    Aug 28, 2006
  5. Raj Singh
    Replies:
    2
    Views:
    212
    Rick DeNatale
    May 29, 2008
Loading...

Share This Page