I have a problem with a VB.net Program (Visual Studio 2010). It creates a new Word-File, opens other Word-files (on a form the user can choose which), selects and copies their content and pastes it into the new file, so basically you have one file containing the content of all the others.
With a certain amount of files it works pretty well. However, if a user selects all of them (121) the program eventually crashes after ~3 minutes with the RPC-E-SYS-CALL-FAILED exception. I tried to google it and I already know that it has to do something with Threads or MessageQueue but I'm pretty new to VB.Net and would like to have a newbie-explanation of what is happening, why it's happening and what I have to do to optimize my code.
Relevant code:
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Word
Imports Microsoft.VisualBasic
Public Class Generator
'Do stuff
'Relevant Sub:
Private Sub CreateDocument()
Dim word As New Microsoft.Office.Interop.Word.Application
Dim doc_gesamt As Microsoft.Office.Interop.Word.Document
doc_gesamt = word.Documents.Add()
doc_gesamt.Activate()
doc_gesamt.Range(0, 0).Select()
For i = 0 To (Modul_Global.analysen.Rows.Count - 1) Step 1
Dim wordtemp As New Microsoft.Office.Interop.Word.Application
Dim doctemp As Microsoft.Office.Interop.Word.Document
doctemp = wordtemp.Documents.Open(filepath & Modul_Global.analysen.Rows(i)("filename"))
doctemp.Activate()
doctemp.Range.WholeStory()
doctemp.Range.Copy()
doc_gesamt.Activate()
word.Selection.EndKey(Unit:=Microsoft.Office.Interop.Word.WdUnits.wdStory)
word.Selection.PasteAndFormat(wordoptions.WdRecoveryType.wdFormatOriginalFormatting)
word.Selection.InsertBreak(Type:=Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak)
word.Selection.EndKey(Unit:=Microsoft.Office.Interop.Word.WdUnits.wdStory)
doctemp.Close(False)
wordtemp.Quit()
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(doctemp)
Next
Clipboard.Clear()
word.Visible = True
doc_gesamt.Activate()
End Sub
End Class