Search code examples
pythoncomwin32com

Using win32com to connect to a new rather than existing/running instance of a COM server


I am using the win32com package to drive Excel as a background calculation engine:

import win32com.client as wc

#Get a dispatch interface for the Excel app
_xl = wc.Dispatch('Excel.Application')

This is fine as far as it goes, but Dispatch will connect to an existing instance of Excel if one is already running. If the user already has Excel open and is working on something then this may cause problems.

Is there a way to specify that I want a new instance of Excel regardless, and create a dispatch interface to that instance?

In a ideal world I would keep that instance as 'private', ie it does not get added to the Running Object Table (ROT) and no other process can get an interface to it.


Solution

  • You can use win32com.client.DispatchEx("Excel.Application"), with the DispatchEx rather than just Dispatch. That seemed to work for me to open it as a separate instance, not sure about the ROT though (no idea what that is).


    I knew I had bumped into this question before, but in reverse: What is the difference between Dispatch and DispatchEx.