Q:

Excel Add-In Form Sends Excel to Background When Form Closes

I have an Excel Add-In that pops up a form. If the user fills in some values and clicks the "Apply" button, the values are correctly sent to the underlying worksheet and the pop-up form closes. If, however, the user clicks on "Cancel," a Messagebox pops-up verifying that the user wants to cancel. Clicking "Yes" causes the pop-up form to close, but also sends Excel behind the previously active window -- very much like pressing Alt-Tab. This is not good!
Does anybody have any notions about how to keep Excel on top?
Thanks!
Randy

A:

I looked for all sorts of variations on ActiveWindow/ActiveView but couldn't find anything applicable. Finally I went around the Mulberry bush to come up with this:
        IntPtr excelWindow = excelWindow = SpreadsheetApplicationFactoryBase.TheExcelApp.ExcelWin32Window.Handle;
        Win32.SetActiveWindow(excelWindow);

Ugly, but it works. The factory is a small class that creates/returns the Excel instance. 
I very much appreciate the input. 
Randy

