I am trying to combine few word docs and create one final doc using VB.NET as a part of windows application. The word document is getting disconnected from RPC while trying to edit the final resulting document.
Here is a code sample showing what I am trying to do,
<code> 'Create a new Word application
Dim wordApp As Word.Application = Nothing
wordApp = New Word.Application
'Create a new final document
Dim finalDocument As Word.Document = Nothing
Try
'Create a new final document
finalDocument = wordApp.Documents.Add()
'Adding Initial template doc
Dim doc_Template_File As Word.Document =wordApp.Documents.Open(doc_Template_FilePath)
'Do some editing
'Copy the content from one file
doc_Template_File.Content.Copy()
If finalDocument IsNot Nothing Then
finalDocument.Range.Paste() 'Getting the exception at this line
finalDocument.Range.InsertParagraphAfter()
doc_Template_File.Close(SaveChanges:=False)
End If
Catch ex As Exception
'Handle exception
Finally
' Release resources
If finalDocument IsNot Nothing Then finalDocument.Close(SaveChanges:=False)
If wordApp IsNot Nothing Then
wordApp.Quit()
helper.ReleaseComObject(wordApp)
End If
End Try
</code>
<code> 'Create a new Word application
Dim wordApp As Word.Application = Nothing
wordApp = New Word.Application
'Create a new final document
Dim finalDocument As Word.Document = Nothing
Try
'Create a new final document
finalDocument = wordApp.Documents.Add()
'Adding Initial template doc
Dim doc_Template_File As Word.Document =wordApp.Documents.Open(doc_Template_FilePath)
'Do some editing
'Copy the content from one file
doc_Template_File.Content.Copy()
If finalDocument IsNot Nothing Then
finalDocument.Range.Paste() 'Getting the exception at this line
finalDocument.Range.InsertParagraphAfter()
doc_Template_File.Close(SaveChanges:=False)
End If
Catch ex As Exception
'Handle exception
Finally
' Release resources
If finalDocument IsNot Nothing Then finalDocument.Close(SaveChanges:=False)
If wordApp IsNot Nothing Then
wordApp.Quit()
helper.ReleaseComObject(wordApp)
End If
End Try
</code>
'Create a new Word application
Dim wordApp As Word.Application = Nothing
wordApp = New Word.Application
'Create a new final document
Dim finalDocument As Word.Document = Nothing
Try
'Create a new final document
finalDocument = wordApp.Documents.Add()
'Adding Initial template doc
Dim doc_Template_File As Word.Document =wordApp.Documents.Open(doc_Template_FilePath)
'Do some editing
'Copy the content from one file
doc_Template_File.Content.Copy()
If finalDocument IsNot Nothing Then
finalDocument.Range.Paste() 'Getting the exception at this line
finalDocument.Range.InsertParagraphAfter()
doc_Template_File.Close(SaveChanges:=False)
End If
Catch ex As Exception
'Handle exception
Finally
' Release resources
If finalDocument IsNot Nothing Then finalDocument.Close(SaveChanges:=False)
If wordApp IsNot Nothing Then
wordApp.Quit()
helper.ReleaseComObject(wordApp)
End If
End Try
The exception is Source:: Microsoft.Office.Interop.Word Source:: System.Runtime.InteropServices.COMException (0x80010108): The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))
I have been trying to figure out things but couldn’t find anything. What am I missing here?