none
Word document not getting focus back after dialog closed RRS feed

  • Question

  • I have a button on my custom ribbon that opens a dialog using the:

    dialog.ShowDialog();

    The problem is when the form is closed and disposed, the focus not returned to the document, so whenever I can't type and move the cursor (unless I click on the document and it gets the focus).

    I know I can solve this by using the:

    dialog.Show();

    method, but I want my dialog to be modal.

    I also tried attaching this event:

    private void TagManager_FormClosed(object sender, FormClosedEventArgs e) { Globals.ThisAddIn.Application.ActiveWindow.SetFocus();

    Globals.ThisAddIn.Application.ActiveDocument.Activate(); }

    But with no luck!

    Any suggestions?

    Tuesday, December 31, 2013 3:47 PM

Answers

  • Hello etasio,

    Did you try to specify the parent window for the ShowDialog method? Note, you need to provide an instance of the  System.Windows.Forms.IWin32Window interface (not the Word window). For example, you need to instantiate a sample class which implement the IWin32Window interface and pass this instance to the ShowDialog method:

    /// <summary> /// Wrapper class so that we can return an IWin32Window given a hwnd /// </summary> public class WindowWrapper : System.Windows.Forms.IWin32Window { public WindowWrapper(IntPtr handle) { _hwnd = handle; } public IntPtr Handle {

    get { return _hwnd; } } private IntPtr _hwnd; }

    The Word Object Model provides Window class class where you can get the real Hwnd and pass it to the ctor of the WindowWrapper class.
    Tuesday, December 31, 2013 4:56 PM

All replies

  • Hello etasio,

    Did you try to specify the parent window for the ShowDialog method? Note, you need to provide an instance of the  System.Windows.Forms.IWin32Window interface (not the Word window). For example, you need to instantiate a sample class which implement the IWin32Window interface and pass this instance to the ShowDialog method:

    /// <summary> /// Wrapper class so that we can return an IWin32Window given a hwnd /// </summary> public class WindowWrapper : System.Windows.Forms.IWin32Window { public WindowWrapper(IntPtr handle) { _hwnd = handle; } public IntPtr Handle {

    get { return _hwnd; } } private IntPtr _hwnd; }

    The Word Object Model provides Window class class where you can get the real Hwnd and pass it to the ctor of the WindowWrapper class.
    Tuesday, December 31, 2013 4:56 PM
  • The following VB code works well:

    Dim frma As frmAbout = New frmAbout

    Dim ad As Document = Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument)

    frma.ShowDialog()

    ad.Windows.Application.ActiveWindow.SetFocus()

    • Proposed as answer by Hsm Tuesday, July 14, 2015 5:59 PM
    Tuesday, July 14, 2015 5:59 PM