Control 2 separate instances of Excel COM independently ... can this be done?

I have a legacy application that is implemented in several Excel workbooks. It is not that I have the authority to re-deploy, however, another application that I support should be able to call functions in an Excel workbook.

He was given a python interface using the Win32Com library. Other processes can call functions in my python package, which in turn calls the functions that I need through Win32Com.

Unfortunately, COM does not allow me to specify a specific COM process, so at the moment, no matter how powerful my server I can control only one instance of Excel at a time on the computer. If I tried to run more than one excel instance, there would be no way to guarantee that the python layer is bound to a specific Excel instance.

I would like to be able to run more than one of my excel applications on my Windows server at the same time. Is there any way to do this? For example, is it possible to split my environment so that I can run as many Excel _ Python combinations as my application will support?

+6
python windows excel com
source share
3 answers

I don't know anything about Python, unfortunately, but if it works through COM, Excel is not a single-instance application, so you should be able to create as many Excel instances as memory allows.

With C #, you can create multiple instances of Excel with:

Excel.Application xlApp1 = new Excel.Application(); Excel.Application xlApp2 = new Excel.Application(); Excel.Application xlApp3 = new Excel.Application(); 

Using the latest binding in C #, you can use:

 object objXL1 = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application")); object objXL2 = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application")); object objXL3 = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application")); 

If you use VB.NET, VB6, or VBA, you can use CreateObject as follows:

 Dim objXL1 As Object = CreateObject("Excel.Application") Dim objXL2 As Object = CreateObject("Excel.Application") Dim objXL3 As Object = CreateObject("Excel.Application") 

Unfortunately in Python I don't have a hint. But is there any kind of limitation for Python (which I can’t imagine?), Then I would think that it is very useful.

Nevertheless, the idea that several Excel instances act as some kind of server for other operations sounds pretty smooth ... I would try to check what you are doing, especially regarding how many instances you can open right away without running out of memory and what happens to the calling program if Excel for some reason works.

+2
source share

See “Launching a New Instance of a COM Application” by Tim Gold , also a link here for tips on using

 xl_app = DispatchEx("Excel.Application") 

but not

 xl_app = Dispatch("Excel.Application") 

to start a separate process. Therefore, you should be able to:

 xl_app_1 = DispatchEx("Excel.Application") xl_app_2 = DispatchEx("Excel.Application") xl_app_3 = DispatchEx("Excel.Application") 

Note that when you are done to close the application, I have found it necessary:

 xl_app.Quit() xl_app = None 

I found that the process will not be closed until xl_app = None , that is, the count of the reference to the Python object object will be zero.

Note I have one remaining problem: while my Python program is running, if I double-click the Excel file in Explorer (at least on Win2k), it finishes opening the file in an existing Excel process that was started by Python (which runs hidden), which disrupts the Python program. I have not yet found for this resolution.

+5
source share

If the application uses a single excel file that contains macros that you call, I am afraid that the answer is probably missing, because aside from COM Excel is not allowed to open the same file with the same name (even if in different directories ), You can get around this by dynamically copying the file to a different name before opening it.

My python knowledge is not huge, but in most languages ​​there is a way to indicate when you create a COM object whether you want it to be a new object or connect to an existing default instance. Check out the python docs for something on these lines.

Can you talk about the specific problems you have and what you hope to do?

0
source share

All Articles