Create another Excel instance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yvan

    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_flag s = 0
    pythoncom.CoIni tializeEx(pytho ncom.COINIT_MUL TITHREADED)
    import win32com.client .dynamic
    self.xlApp = win32com.client .dynamic.Dispat ch("Excel.Appli cation")
    self.xlApp.Visi ble = bVisible
    self.xlBook = self.xlApp.Work books.Add()
    self.xlSheet = self.xlApp.Acti veSheet

    def __del__(self):
    import pythoncom
    if self.xlSheet != None:
    del(self.xlShee t)
    if self.xlBook != None:
    self.xlBook.Clo se(0)
    del(self.xlBook )
    if self.xlApp != None:
    self.xlApp.Quit ()
    del(self.xlApp)
    pythoncom.CoUni nitialize()

    Thank for your help,

    -Yvan
  • Cy Edmunds

    #2
    Re: Create another Excel instance

    "yvan" <yvan_charpenti [email protected]> wrote in message
    news:9ee55987.0 307101339.70196 [email protected] gle.com...[color=blue]
    > I am using Excel to save data.
    > Everything works as i intend it to if no other instance of Excel is[/color]
    running.[color=blue]
    > If another instance is running, it will do the job, but also close that[/color]
    instance.[color=blue]
    > 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_flag s = 0
    > pythoncom.CoIni tializeEx(pytho ncom.COINIT_MUL TITHREADED)
    > import win32com.client .dynamic
    > self.xlApp = win32com.client .dynamic.Dispat ch("Excel.Appli cation")
    > self.xlApp.Visi ble = bVisible
    > self.xlBook = self.xlApp.Work books.Add()
    > self.xlSheet = self.xlApp.Acti veSheet
    >
    > def __del__(self):
    > import pythoncom
    > if self.xlSheet != None:
    > del(self.xlShee t)
    > if self.xlBook != None:
    > self.xlBook.Clo se(0)
    > del(self.xlBook )
    > if self.xlApp != None:
    > self.xlApp.Quit ()
    > del(self.xlApp)
    > pythoncom.CoUni nitialize()
    >
    > Thank for your help,
    >
    > -Yvan[/color]

    I haven't tried this myself but maybe...

    if self.xlApp != None:
    nbook = self.xlApp.Work books.Count # number of open workbooks
    if nbook == 0:
    self.xlApp.Quit ()

    Let us know how you made out.

    --
    Cy



    Comment

    • yvan

      #3
      Re: Create another Excel instance

      "Cy Edmunds" <cedmunds@spaml ess.rochester.r r.com> wrote in message news:<FZmPa.289 43$EQ5.7055@twi ster.nyroc.rr.c om>...[color=blue]
      > "yvan" <yvan_charpenti [email protected]> wrote in message
      > news:9ee55987.0 307101339.70196 [email protected] gle.com...[color=green]
      > > I am using Excel to save data.
      > > Everything works as i intend it to if no other instance of Excel is[/color]
      > running.[color=green]
      > > If another instance is running, it will do the job, but also close that[/color]
      > instance.[color=green]
      > > 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_flag s = 0
      > > pythoncom.CoIni tializeEx(pytho ncom.COINIT_MUL TITHREADED)
      > > import win32com.client .dynamic
      > > self.xlApp = win32com.client .dynamic.Dispat ch("Excel.Appli cation")
      > > self.xlApp.Visi ble = bVisible
      > > self.xlBook = self.xlApp.Work books.Add()
      > > self.xlSheet = self.xlApp.Acti veSheet
      > >
      > > def __del__(self):
      > > import pythoncom
      > > if self.xlSheet != None:
      > > del(self.xlShee t)
      > > if self.xlBook != None:
      > > self.xlBook.Clo se(0)
      > > del(self.xlBook )
      > > if self.xlApp != None:
      > > self.xlApp.Quit ()
      > > del(self.xlApp)
      > > pythoncom.CoUni nitialize()
      > >
      > > Thank for your help,
      > >
      > > -Yvan[/color]
      >
      > I haven't tried this myself but maybe...
      >
      > if self.xlApp != None:
      > nbook = self.xlApp.Work books.Count # number of open workbooks
      > if nbook == 0:
      > self.xlApp.Quit ()
      >
      > Let us know how you made out.[/color]

      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?

      Comment

      • David Rushby

        #4
        Re: Create another Excel instance

        yvan_charpentie [email protected] (yvan) wrote in message news:<9ee55987. 0307111159.6978 [email protected] ogle.com>...
        [color=blue]
        > 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?[/color]

        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.Ensure Module('{000208 13-0000-0000-C000-000000000046}', 0, 1, 2)
        xlApplicationCl ass = gencache.GetCla ssForProgID('Ex cel.Application ')

        # Create a new instance of the Excel.Applicati on class--don't reuse an
        # existing instance.
        xlApp = xlApplicationCl ass()

        xlApp.Visible = False
        xlApp.DisplayAl erts = False

        try:
        wbk = xlApp.Workbooks .Add()
        sht = wbk.Sheets(1)

        sht.Cells(1,1). Value = 'blah'
        raise Exception('Deli berately raise an exc to test tidiness of cleanup.')
        finally:
        try:
        wbk.Close()
        del wbk
        except:
        pass

        xlApp.Quit()
        del xlApp
        ---------------------------------------------------------------

        Comment

        Working...