Well it's a long time since I hosted Word inside a form panel ... VB6 in fact! But one of the issues is that Notepad is a single-window application, so the main executable just has a main window and you can take parentage of that. Word is a multi-document-interface application, and the executable launches a child window for each document displayed. (HINT: Open task manager, and then launch Word. You'll see one instance of Word. Within word, create a new document, then another. Task manager still shows a SINGLE copy of word, but with three separate windows listed. Do the same with Notepad - you'll see three separate instances of the executable running). One of the issues is that if Word is already running, launching it the way you are may get the wrong window, or none at all. In fact with no existing instance of Word running, your code seems (sometimes!) to work fine for me, with the document appearing in the panel on my form.
To do what you want, you're better to have Word open a specific document at startup, and then you can take ownership of the primary window (assuming Word is the default application for .doc files):
proc = Process.Start("C:\MyFolder\MyDocument.doc")
Alternatively, you can explicitly request winword.exe and pass the filename as command line parameters:
dim startInfo as new ProcessStartInfo("winword.exe")
startInfo.Arguments = "/q /t c:\users\sos\test.doc"
proc = Process.Start(startInfo)
(Here,
\q
specifies "quiet mode", so doesn't show the Word splash screen; the
\t
switch specifies the next parameter is the file to open.) The rest of your code is unchanged.
However, there will be problems if you close your application without first closing Word, and you may end up creating recovery files for your documents. You can also run into the problem that if the user clicks the "Minimise" or "Restore" buttons on Word's title bar, that Word will shrink and not be visible within your panel - irretrievably. It looks like maximising doesn't work quite right, either!
The better way to do this is probably to fully automate Word, and for that you'll need to use the Interop libraries and directly manipulate Word and it's window(s). This also has the advantage that you can manage multiple document windows, access document contents and manipulate them, show / hide toolbars / ribbons and so on. However it's quite a big step up from the simple code above. Google "automate word from vb.net". Using that method you'll most likely need to search for the correct window handle to take ownership of; use the Win32 function
FindWindowEx
passing in the title of the Word form. Depending on the version of Windows and Word, this is either "
test.doc - Microsoft Word
" or "
Microsoft Word - test.doc
". Once you've got the right window handle, you can take ownership as you do above.