Win32com dispatch won't find an already open application instance

I am using python to parse an Excel file and access the COM application with excel = Dispatch('Excel.Application')

at the start of restarting the code that will find the application object very well and I can access the active workbook.

The problem occurs when I had two instances of Excel and I closed the first one. From now on, each call excel = Dispatch('Excel.Application')

exposes an application object other than the open instance of Excel. If I try it excel.Visible=1

, it will open a new instance of Excel instead of showing an already open instance of excel. How to get the COM object of an already open instance of Excel instead of creating a new instance?

+3


source to share


1 answer


When an application registers, only the first instance is registered until it dies, and then the very next instance is registered to register.

There is no registration queue, so when your first instance dies, the second is kept unregistered, so any call Excel.Application

will start the third instance and they will continue to use it until it dies.

Thus, instances launched between registered instances are never registered.



If you need to reuse an instance, you must store a pointer to it.

However, if you get a copy of an open Excel file, you might get a link to an unregistered Excel copy. For example, if in Excel 1 (registered) desktop 1 is open, and in Excel 2 (unregistered) workbook 2 is opened, if you query for workbook 2, you will get an instance of Excel 2 (for example through Workbook.Application

).

+2


source







All Articles