Retrieve Multiple Excel Instances

C

CA

I am able to obtain a running instance of Excel using the following
code.

<code>
Excel.Application app = (Excel.Application)
Marshal.GetActiveObject("Excel.Application");
</code>

Usually this retrieves the first instance of Excel that was opened.
Suppose there is more than one instance of Excel running on a given
machine. Is it possible to get a reference to each of these instances?

Any help would be greatly appreciated.

Thanks

Chris
 
Y

Ying-Shen Yu[MSFT]

Hi Chris,

You may try to retrieve the active Excel applications by enumerating the
ROT table directlly.
The enumeration needs use some Windows API by P/Invoke.
Here is a small sample, it will show you how to enumerate the
RunningObjectsTable, for more information you may take a look at these APIs
in MSDN.

<code>
[DllImport("ole32.dll")]
public static extern int GetRunningObjectTable(int reserved, out
UCOMIRunningObjectTable prot);

[DllImport("ole32.dll")]
public static extern int CreateBindCtx(int reserved, out UCOMIBindCtx
ppbc);

public object[] GetActiveObjectsFromROT(string moniker)
{
ArrayList objs = new ArrayList();
UCOMIRunningObjectTable prot;
UCOMIEnumMoniker pMonkEnum;

GetRunningObjectTable(0,out prot);
prot.EnumRunning(out pMonkEnum);
pMonkEnum.Reset();
int fetched;

UCOMIMoniker []pmon = new UCOMIMoniker[1];
while(pMonkEnum.Next(1, pmon, out fetched) == 0)
{
UCOMIBindCtx pCtx;
CreateBindCtx(0, out pCtx);
string str;

pmon[0].GetDisplayName(pCtx,null,out str);
System.Diagnostics.Debug.WriteLine(str);
if(str == moniker)
{
object objReturnObject;
prot.GetObject(pmon[0],out objReturnObject);
objs.Add(objReturnObject);
}
}
return objs.ToArray();
}
</code>

Does it resolve your problem?

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
C

CA

Ying-Shen,

Using the steps that you suggested, I was able to retrieve a handle to
the running processes.

Thanks for your help.

Best Regards

Chris
 

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