Excel PID process casting

  • Thread starter Thread starter retsb
  • Start date Start date
R

retsb

I have an Excel workbook that launch an external .exe. My .exe is
written in c#. My application need a reference to the excel
applicationthat just call it. When calling the external .exe (my c#
application) from Excel, i'm passing the Excel PID in arguments. In c#
i'm able to get the Excel process with Process.GetProcessById(pid) but
i don't know how to cast it back to an Excel application Object. ex :
appExcel = (Excel.Application)(Process.GetProcessById(pid)) this
doesn't work.

I need something like that but with the process pid :
appExcel =
(Excel.Application)Marshal.GetActiveObject("Excel.Application");

Thanks,
Tommy
 
Tommy,

What you should do from Excel is pass the name of the workbook to your
process. With that, you should be able to pass that to the
CreateFileMoniker function through the P/Invoke layer. This will give you
an IMoniker interface (use the definition from the
System.Runtime.InteropServices.ComTypes namespace for the IMoniker
definition) which you can then pass to the GetObject method on the
IRunningObjectTable interface. You get the IRunningObjectTable interface by
calling the GetRunningObjectTable function through the P/Invoke layer.

You would pass the path of the workbook to the GetObject method, and you
should be able to cast what you get back into something usable (either the
workbook instance, or the application instance).
 

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

Back
Top