views:

131

answers:

1

In my winforms app interacting with Excel through the com interop,

I'm trying to attach to an existing Excel process if there is one. Getting the object seems to work well, but if the Excel application is minimized (which is quite likely in my use case), I don't manage to restore the window and bring it to the front.

I've tried the following statements:

try
    app = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
catch (Exception) { /* ignore */ }

if (app == null)
{
    app = new Excel.Application();
    app.Visible = true;
}

if (app.ActiveWindow.WindowState == Excel.XlWindowState.xlMinimized)
    app.ActiveWindow.WindowState = Excel.XlWindowState.xlNormal;

wb = ...

wb.Activate();

None of these had any effect. How can I achieve that?

(Please Note: My problem relates to the case when there is an existing instance, so the "Visible = true" is not necessary and this thread does not apply.)

+1  A: 

You want app.WindowState = xlNormal as app.ActiveWindow is the current sheet not the application instances main window.

Alex K.
Alex, it couldn't be simpler! Wonder how I couldn't find this on my own, since I even searched on the application class first for an appropriate method... (probably I was looking for an "Activate()" method then)
chiccodoro