Click here to Skip to main content
14,579,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to show Microsoft word in a panel in vb.net 2010.
How to do any suggestion please

What I have tried:

Imports Microsoft.Office.Interop
Public Class form1
    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 Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        proc = Process.Start("notepad.exe")
        proc.WaitForInputIdle()
        SetParent(proc.MainWindowHandle, Panel1.Handle)
        SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    End Sub
End Class



i have typed winword.exe in process.start. it is starting microsoft word but not in panel.
if i type notepad.exe in process.start it opens in panel.
Posted
Updated 29-Jun-20 13:16pm
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100