Search code examples
c#.netexceloffice-interopexcel-interop

Make window a child of Interop.Excel Com object


This is very nearly what I need: How to open a child windows under parent window on menu item click in WPF?

However, my parent object in this case is an Office.Interop.Excel com object while my child object is a System.Windows.Window object. I am wondering if there is a way to get or cast a Window object from the interop.excel object.

The behavior I am looking for is that the window that popups on the excel sheet should be topmost for the excel application only. Setting the property TopMost doesn't work here as the window ends up always being the focus.


Solution

  • Shoutout to Hans Passant who directed me to the answer in the comment above.

    Excel's Application.Hwnd gave me the handle for the excel window. To convert that to a System.Windows.Window object I used the following:

    var helper = new System.Windows.Interop.WindowInteropHelper(view);
    helper.Owner = (System.IntPtr)excel.Application.Hwnd;
    

    In this way, my view is now designated as a child of the main excel window.