automate Excel in C#

M

Michael Wu

I wonder if anyone can shed some light on how to automate Excel from a C#
program.

What I like to do is this,
1) Find if an Excel spreadsheet is alreay opened (identify the Excel file
path)
2) if that particular Excel is alreay launch, get the Excel Object
3) run a macro on the Excel Object

I made some progress using

tlbimp on Excel.exe to get the interop lib
Excel._Application excelApp = (Excel._Application)
System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");

to get an Excel object. But this method, does not allow me to get a
particular Excel object. If I have more than one Excel session open, I think
I always got the one opened most recently. I thne tried the win32
GetObject(string, string), but this one alway generate a runtime error
System.InteropServices.MarshalDirectiveException : PInvoke restriction: can
not return variants.

Any help will be greatly appreciated.

Regards

Michael
 
J

Jens Thiel

Michael Wu said:
I wonder if anyone can shed some light on how to automate Excel from a C#
program.

What I like to do is this,
1) Find if an Excel spreadsheet is alreay opened (identify the Excel file
path)
2) if that particular Excel is alreay launch, get the Excel Object

You need to get the object from the ROT (running object table).

Jens.
 

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