First off i'm not quite sure if this question belongs in this section or not, so if a mod feels it would better answered somewhere else please move it.
So I am trying to host a WinWord 2010 app in my vb.net application. After reading allot I found that this cannot really be done, but what could be done is almost mimic the same thing as the old oleControl that vb6 had to host excel and word.
By using some API's we can set the parent of the window that the Word app is in to a window in my application. I have tested this code against Word 2003 and it does indeed work ok, however when I ran it against a Word 2010 installation it kind-of fails as the bottom of the Word app is cut off.
I can get it to work correctly but not the way I want it to work. I wish to have some space above the Word window for some other controls that I'm gonna have to insert fields into the document and such.
Any ideas how to position this Word window within my panel?
Here is the complete code:
Create a project: add a winForm name clsEmbedWord, add a panel named WordPanel, Add a groupbox-docked to top of form.
So I am trying to host a WinWord 2010 app in my vb.net application. After reading allot I found that this cannot really be done, but what could be done is almost mimic the same thing as the old oleControl that vb6 had to host excel and word.
By using some API's we can set the parent of the window that the Word app is in to a window in my application. I have tested this code against Word 2003 and it does indeed work ok, however when I ran it against a Word 2010 installation it kind-of fails as the bottom of the Word app is cut off.
I can get it to work correctly but not the way I want it to work. I wish to have some space above the Word window for some other controls that I'm gonna have to insert fields into the document and such.
Any ideas how to position this Word window within my panel?
Here is the complete code:
Create a project: add a winForm name clsEmbedWord, add a panel named WordPanel, Add a groupbox-docked to top of form.
Code:
Option Strict Off
Imports Microsoft.Office.Interop.Word
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices
Public Class clsEmbedWord
#Region "Enums"
<Flags()> _
Private Enum SetWindowPosFlags As UInteger
''' <summary>If the calling thread and the thread that owns the window are attached to different input queues,
''' the system posts the request to the thread that owns the window. This prevents the calling thread from
''' blocking its execution while other threads process the request.</summary>
''' <remarks>SWP_ASYNCWINDOWPOS</remarks>
SynchronousWindowPosition = &H4000
''' <summary>Prevents generation of the WM_SYNCPAINT message.</summary>
''' <remarks>SWP_DEFERERASE</remarks>
DeferErase = &H2000
''' <summary>Draws a frame (defined in the window's class description) around the window.</summary>
''' <remarks>SWP_DRAWFRAME</remarks>
DrawFrame = &H20
''' <summary>Applies new frame styles set using the SetWindowLong function. Sends a WM_NCCALCSIZE message to
''' the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE
''' is sent only when the window's size is being changed.</summary>
''' <remarks>SWP_FRAMECHANGED</remarks>
FrameChanged = &H20
''' <summary>Hides the window.</summary>
''' <remarks>SWP_HIDEWINDOW</remarks>
HideWindow = &H80
''' <summary>Does not activate the window. If this flag is not set, the window is activated and moved to the
''' top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter
''' parameter).</summary>
''' <remarks>SWP_NOACTIVATE</remarks>
DoNotActivate = &H10
''' <summary>Discards the entire contents of the client area. If this flag is not specified, the valid
''' contents of the client area are saved and copied back into the client area after the window is sized or
''' repositioned.</summary>
''' <remarks>SWP_NOCOPYBITS</remarks>
DoNotCopyBits = &H100
''' <summary>Retains the current position (ignores X and Y parameters).</summary>
''' <remarks>SWP_NOMOVE</remarks>
IgnoreMove = &H2
''' <summary>Does not change the owner window's position in the Z order.</summary>
''' <remarks>SWP_NOOWNERZORDER</remarks>
DoNotChangeOwnerZOrder = &H200
''' <summary>Does not redraw changes. If this flag is set, no repainting of any kind occurs. This applies to
''' the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent
''' window uncovered as a result of the window being moved. When this flag is set, the application must
''' explicitly invalidate or redraw any parts of the window and parent window that need redrawing.</summary>
''' <remarks>SWP_NOREDRAW</remarks>
DoNotRedraw = &H8
''' <summary>Same as the SWP_NOOWNERZORDER flag.</summary>
''' <remarks>SWP_NOREPOSITION</remarks>
DoNotReposition = &H200
''' <summary>Prevents the window from receiving the WM_WINDOWPOSCHANGING message.</summary>
''' <remarks>SWP_NOSENDCHANGING</remarks>
DoNotSendChangingEvent = &H400
''' <summary>Retains the current size (ignores the cx and cy parameters).</summary>
''' <remarks>SWP_NOSIZE</remarks>
IgnoreResize = &H1
''' <summary>Retains the current Z order (ignores the hWndInsertAfter parameter).</summary>
''' <remarks>SWP_NOZORDER</remarks>
IgnoreZOrder = &H4
''' <summary>Displays the window.</summary>
''' <remarks>SWP_SHOWWINDOW</remarks>
ShowWindow = &H40
End Enum
#End Region
Public Const HWND_TOPMOST = -1
' Retrieves a handle to the top-level window whose class name and window
' name match the specified strings. This function does not search
' child windows. This function does not perform a case-sensitive search.
<DllImport("user32.dll", EntryPoint:="FindWindow", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindowByCaption( _
ByVal zero As IntPtr, _
ByVal lpWindowName As String) As IntPtr
End Function
' Changes the parent window of the specified child window.
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
End Function
' Changes the size, position, and Z order of a child, pop-up, or top-level window.
' These windows are ordered according to their appearance on the screen.
' The topmost window receives the highest rank and is the first window in the Z order.
<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, _
ByVal hWndInsertAfter As IntPtr, _
ByVal X As Integer, ByVal Y As Integer, _
ByVal cx As Integer, ByVal cy As Integer, _
ByVal uFlags As SetWindowPosFlags) As Boolean
End Function
Private WithEvents ObjWord As Application
Private WordWND As Integer
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
ObjWord.Quit(False)
End Sub
Private Sub clsEmbedWord_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Me.WordPanel.Top = Me.Top + Me.GroupBox1.Height + 10
Me.WordPanel.Height = Me.Height - Me.GroupBox1.Height
Try
Me.Cursor = Cursors.AppStarting
' Load Word and make invisible
ObjWord = New Word.Application
ObjWord.Visible = False
' Set a custom window title to make it
' easier to find the window handle
ObjWord.Caption = "I.G.O.R.- WORD"
WordWND = FindWindowByCaption(vbNullString, "I.G.O.R.- WORD")
' Make Word a child window of the picture control
SetParent(WordWND, Me.WordPanel.Handle.ToInt32())
ObjWord.WindowState = Word.WdWindowState.wdWindowStateNormal
' Positon Word to the coordinates of the picture control
SetWindowPos(WordWND, HWND_TOPMOST, 0, 0, Me.WordPanel.Bounds.Width, Me.WordPanel.Bounds.Height - 20, Nothing)
' Add a document and make Word visible
ObjWord.Documents.Add()
ObjWord.Visible = True
Me.Cursor = Cursors.Arrow
Catch Ex As Exception
MsgBox(Ex.ToString, MsgBoxStyle.OkOnly Or MsgBoxStyle.SystemModal, "Exception")
End Try
End Sub
End Class