Multiple Excel processes

C

cnathaide

I am trying to control an existing running process from C#. More
specifically, I am trying to control an existing workbook that is open
in memory. I use the following code

using System.Runtime.InteropServices;

Excel.Application xlApp = Marshal.GetActiveObject("Excel.Application");

This works fine if there is only one Excel process running. Typically
the users have multiple Excel processes running. Is there any way to
iterate through all the running processes and get all the copies of
Excel that are running.

Thanks

Chris
 
F

Francois Bonin [MVP]

Have you looked at the System.Diagnostics.Process class?
It offers a static method GetProcessesByName(string)
to which you can pass "Excel.exe" to retrieve all instances of excel
 
C

cnathaide

Assume that I get an array of such processes using this method. How do
I convert the process object into an object of type
"Excel.Application"?

Thanks

Chris
 
W

Willy Denoyette [MVP]

Assume that I get an array of such processes using this method. How do
I convert the process object into an object of type
"Excel.Application"?

Thanks

Chris

You can't connect to a "specific" instance of an application when using
GetActiveObject, if you need this you will have to create a new instance.

Willy.
 
C

cnathaide

I am trying to get a handle to the workbook that is already open. It
seems that the program works if the workbook is opened from the first
instance of Excel that is created (using
Marshal.GetActiveObject("Excel.Application")), but it does not work if
the workbooks is opened from the 2nd or 3rd instance of Excel. Is there
any way that this can be remedied?

Thanks

Chris
 
W

Willy Denoyette [MVP]

I am trying to get a handle to the workbook that is already open. It
seems that the program works if the workbook is opened from the first
instance of Excel that is created (using
Marshal.GetActiveObject("Excel.Application")), but it does not work if
the workbooks is opened from the 2nd or 3rd instance of Excel. Is there
any way that this can be remedied?

Thanks

Chris

No, you can only connect to the single instance that was registered in the
ROT. If you have 3 instances of the "same" server application running, you
will always connect to the first application that was started as this is the
one registered.
If you need a private instance you will need to create a new instance (using
.... = new Excel.ApplicationClass(); )and not connect to an instance that is
already running. This is the point of automation, you should not connect to
an instance started by an interactive user.

Willy.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top