0
votes

We've inherited some code for a VSTO Outlook Add-In that pops up a modal dialog sort of as an acknowledgement to the end user before sending an email.

The dialog is fired on the Application_ItemSend event

Private Sub Application_ItemSend(ByVal Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend

The problem that we're seeing is that when we show the dialog:

objCheckDialog.ShowDialog()

The outgoing email window is minimized, when the dialog pops-up which is not desirable and using .Show() is also not desirable.

During our research we've seen some issues where it was suggested to investigate parent properties of our dialog object, however we don't see any parent properties available which would allow us to maximize the parent:

enter image description here

Another suggestion was to pass the ShowDialog() a reference to the Add-In to specify an owner of the dialog box, IE:

objCheckDialog.ShowDialog(Me)

Since that property is also Nothing, but thought that this might populate Parent:

enter image description here

However, that throws the following exception:

{"Unable to cast object of type 'XYZ.ThisAddIn' to type 'System.Windows.Forms.IWin32Window'."}

Any idea of what we're doing wrong?

Thanks.

1

1 Answers

1
votes

You would need to use the NativeWindow class.

objCheckDialog.ShowInTaskbar = false;
IntPtr wnd = ParentWindow();
if (wnd != IntPtr.Zero)
{
    NativeWindow nativeWindow = new NativeWindow();
    nativeWindow.AssignHandle(wnd);
    return objCheckDialog.ShowDialog(nativeWindow);
}
else
{
    return form.ShowDialog();
}

Parent windows handle can be retrieved from Explorer or Inspector object using IOleWindow interface:

[ComImport]
[Guid("00000114-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleWindow
{
    void GetWindow(out IntPtr phwnd);
    void ContextSensitiveHelp([In, MarshalAs(UnmanagedType.Bool)] bool fEnterMode);
}

public IntPtr ParentWindow()
{
    IntPtr wnd = new IntPtr(0);
    object window = _application.ActiveWindow();
    if (window != null)
    {
         IOleWindow oleWindow = window as IOleWindow;
         if (oleWindow != null)
         {
             oleWindow.GetWindow(out wnd);
         }
    }
    return wnd;
}