I am trying to figure out a way to open MS Word inside a panel in visual studio 2010. The code I have works for certain .exe files but if I try with word or excel it opens it's own window. Can anyone tell me if this is possible? Here is the code I have:

Public Class MSO2014CC
Declare Auto Function SetParent Lib "user32.dll" (ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As Integer
Declare Auto Function SendMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
Private Const WM_SYSCOMMAND As Integer = 274
Private Const SC_MAXIMIZE As Integer = 61488
Dim proc As Process

Private Sub MSO2014CC_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    proc = Process.Start("C:\Program Files\Microsoft Office\Office14\WINWORD.exe")

    SetParent(proc.MainWindowHandle, Panel1.Handle)
    SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 1269)
End Sub

End Class

Edited 3 Years Ago by pritaeas: Moved

Can anyone tell me if this is possible?

Yes it can be done, but you may not like dealing with some of the after effects of doing it.

First you need to get the correct window handle using the API function FindWindow with the proper class name. For Word this is "OpusApp" and I believe it is "XLMAIN" for Excel.

Would you be able to give me an example like how I would do this with the code I have? Or do I just replace "C:\Program Files\Microsoft Office\Office14\WINWORD.exe" with OpusApp?

This article has been dead for over six months. Start a new discussion instead.