2

This launches a fresh Excel workbook:

        Excel.Application oXL;
        Excel._Workbook oWB;

        oXL = new Excel.Application();
        oXL.Visible = true;

        oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));

However, what if one wants to either...

-- 'GetObject' (in the familiar Automation paradigm) to a workbook that's already loaded and open on the screen?, or

-- access and write data to a closed workbook by path name?

Both of which are doable by old standards. Preferably the latter although I'm not a chooser right now. Thanks for any help.

user225626
  • 1,091
  • 5
  • 16
  • 32
  • Tag more topics specific to your question to attract those who can help you get to the answer. – Franz Payer Jan 25 '11 at 02:40
  • Thanks. How do I do that? Do I have to retype the question and send it to each individual tag, or is there a way to take just this post and assign to it more tags? – user225626 Jan 25 '11 at 03:02

1 Answers1

2

You can get an existing instance of Excel using:

Marshal.GetActiveObject("Excel.Application")

This will throw a COMException if there is no instance of Excel running.

One pitfall of automating an instance of Excel that is visible is that your calls may fail because Excel is busy. You may need to implement IMessageFilter to avoid this problem.

As for accessing a closed workbook by path - you need to open the workbook:

oXL.Workbooks.Open(...)
Joe
  • 122,218
  • 32
  • 205
  • 338
  • Joe, thanks for this Marshal info. Very helpful; I didn't know about it. (About .Open, I tried that before I posted and it had actually launched an instance on screen, so I'll do some research with Marshalling on closed files in a way that keeps them from displaying like I used to be able to do with XL objects in COM.) – user225626 Jan 25 '11 at 07:38
  • It was so long ago I don't remember the methodology used on files the instances of which were never launched and displayed. Maybe in C# the ExcelObj.Visible = false; is intended to yield the same results. Thanks again. – user225626 Jan 25 '11 at 07:52
  • Yes, you should have .Visible=false if you don't want to show the UI – Joe Jan 25 '11 at 08:29