Automating existing instance of Excel from c#

G

Giri

Any tips on how to "attach" to an existing running instance of Excel (2000)
from windows forms code written in C# (bit like GetObject instead of
CreateObject)..

Thanks
 
S

Scott Glasgow

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

Will return the running instance of Excel. You might want to see if there
are any running instances first though using System.Diagnostics.Process
class.
 
G

Giri

Thanks for the info Scott !

It works although I when I reference to Excel my .NET objects seem to
appended with "class". I have
Excel.ApplicationClass and not Excel.Application, or Excel.WorkbookClass and
not just Excel.WorkBook...

Anyone have any ideas why that may be ?

Thanks
 
P

Peter Huang [MSFT]

Hi Giri,

The Excel.Application is an interface while Excel.ApplicationClass is
implement Class. You may have a look at the Excel Object Browse.
You may do that by press Ctrl+Alt+J in the IDE.
You may take a look at the example in the link below.
302815 HOW TO: Handle Events for Excel by Using Visual C# .NET
http://support.microsoft.com/?id=302815

Excel.Application exApp =
(Excel.Application)Marshal.GetActiveObject("Excel.Application");
Excel.Worksheet ws =
(Excel.Worksheet)exApp.ActiveWorkbook.Worksheets.get_Item(1);
ws.Cells[1,1]="100";

Can you post some code about where you use the Excel.ApplicationClass?

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
 

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