How can I create a modeless window which takes Office main window as the owner?

Locked How can I create a modeless window which takes Office main window as the owner?

Locked

  • Sunday, February 08, 2009 12:26 PM
    Moderator
     
     

    How can I create a modeless window which takes Office main window as the owner?




    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

All Replies

  • Sunday, February 08, 2009 12:27 PM
    Moderator
     
     Answered

    We can call Form.Show(IWin32Window) function to show the form to the user with the specified owner. To pass the correct IWin32Window type parameter, we create a NativeWindow object and call NativeWindow.AssignHandle(IntPtr) function which binds the native window object with Office native window. The following code shows how to achieve this in Word:

            public partial class NativeMethods

            {

                [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "FindWindowW")]

                public static extern System.IntPtr FindWindowW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpClassName, [System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string lpWindowName);

            }

     

            private void ThisAddIn_Startup(object sender, System.EventArgs e)

            {

                IntPtr wordWindow = NativeMethods.FindWindowW("OpusApp",this.Application.ActiveWindow.Caption+" - Microsoft Word");

                NativeWindow nativeWindow = new NativeWindow();

                nativeWindow.AssignHandle(wordWindow);

     

                Form childWindow = new Form();

                childWindow.Show(nativeWindow);

            }

    As for the Excel and Outlook version, please refer the following links:

    http://social.msdn.microsoft.com/forums/en-US/vsto/thread/2498af9b-2172-43cf-8616-1863f293ccb8/

    http://www.outlooksharp.de/Resources/HowTos/tabid/67/Default.aspx

    (Related forum thread, http://social.msdn.microsoft.com/forums/en-US/vsto/thread/45a82d73-dfe2-4ad1-a0a6-bac35ae5bd64/ )




    For more FAQ about Visual Studio Tools for Office, please see Visual Studio Tools for Office FAQ




    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Wednesday, April 01, 2009 11:52 AM
    Moderator
     
     

    Add VB version codes,

    Public Class ThisAddIn
        Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
            Dim wordWindow As IntPtr
            wordWindow = NativeMethods.FindWindowW("OpusApp", Me.Application.ActiveWindow.Caption + " - Microsoft Word")
            Dim nativeWindow As New System.Windows.Forms.NativeWindow
            nativeWindow.AssignHandle(wordWindow)
            Dim childWindow As New System.Windows.Forms.Form
            childWindow.Show(nativeWindow)
        End Sub
    End Class

    Partial Public Class NativeMethods
        Declare Function FindWindowW Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As System.IntPtr
    End Class


    We have published a VSTO FAQ recently, you can view them from the entry thread http://social.msdn.microsoft.com/Forums/en/vsto/thread/31b1ffbf-117b-4e8f-ad38-71614437df59. If you have any feedbacks or suggestions on this FAQ, please feel free to write us emails to colbertz@microsoft.com.