Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I am facing one issue related to the word application and document in my application.
I am trying to add document in a single word application instance. I am sharing code below:
There is a user control which is implementing the word application and creating document in it on page load.

Winword as user control

C#
Public Shared wordApp As Microsoft.Office.Interop.Word.Application
Dim document As Document
Dim wordWnd As Integer = 0
<DllImport("user32.dll")> _
    Private Shared Function SetParent(ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer
    End Function

    <DllImport("user32.dll", EntryPoint:="SetWindowPos")> 
    Private Shared Function SetWindowPos(ByVal hWnd As Integer, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, _
	ByVal uFlags As UInteger) As Boolean
        'window-positioning options
    End Function

    <DllImport("user32.dll")> _
    Private Shared Function FindWindow(ByVal strclassName As String, ByVal strWindowName As String) As Integer
    End Function

    <DllImport("user32.dll", EntryPoint:="MoveWindow")> _
    Private Shared Function MoveWindow(ByVal hWnd As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As     	Boolean
    End Function

Public Sub New()
        InitializeComponent()
        LoadDocument("~\Sample.doc")
End Sub

Public Sub LoadDocument(ByVal t_filename As String)
        Dim filename As String
        filename = t_filename
        If wordApp Is Nothing Then
            wordApp = New Word.Application()
        End If

        If wordWnd = 0 Then
            wordWnd = FindWindow("Opusapp", Nothing)
        End If
        If wordWnd <> 0 Then
            SetParent(wordWnd, Me.Handle.ToInt32)
            Dim fileName__1 As Object = filename
            Dim newTemplate As Object = False
            Dim docType As Object = 0
            Dim isVisible As Object = True
            Try
                wordApp.Visible = False
                document = wordApp.Documents.Add(fileName__1, newTemplate, docType, True)
                wordApp.Visible = True
            Catch
            End Try
            Try
                wordApp.ScreenUpdating = True
                wordApp.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdWebView
                SetWindowPos(wordWnd, f_handle, 0, 0, Me.Bounds.Width, Me.Bounds.Height, _
                   SWP_NOZORDER Or SWP_NOMOVE Or SWP_DRAWFRAME Or SWP_NOSIZE)
                deactivateevents = False
                'OnResize()
            Catch
            End Try
        End If
End Sub



And I can using this control on my form as follows.

C#
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 1 To 4
            CreateTab("Tab" + i.ToString, "TAB CREATED" + i.ToString)
        Next
End Sub
Private Sub CreateTab(ByVal TabCaption As String, ByVal memoText As String)
        Dim ftb As WinWord
        Dim tpage As TabPage
        Me.TabControl1.TabPages.Add(TabCaption)
        tpage = TabControl1.TabPages(TabControl1.TabPages.Count - 1)
        ftb = New WinWord
        ftb.Name = " WinWord"
        ftb.Dock = DockStyle.Fill
        tpage.Controls.Add(ftb)
End Sub


I am getting one document out side of the application. For every tab pages its working fine but for tab page 1 its throwing document outside of the application.
Please help me out for solving this issue I need to show every tab page with word document with single instance of the word application.

Thanks
Piyush
Posted
Comments
Zoltán Zörgő 25-May-12 9:00am    
Might be related to the array indices. Check if in all cases the first index is the same (0 or 1), if not, be sure to use the proper shifting.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900