0

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
gunr2171
  • 16,104
  • 25
  • 61
  • 88
Broco
  • 217
  • 2
  • 18
  • 1
    Word is an *expensive* program. You started 120 instances of it. The loud bang you heard was the sound of the operating system running out of resources. And no, that FinalReseleaseComObject() call [does not work](http://stackoverflow.com/a/17131389/17034). Just use **one**, it is enough to get the job done. – Hans Passant Jul 01 '14 at 13:51
  • Can you give me a hint what to do instead? As I said, I'm new to .Net-programming. What do you mean with just use **one**? – Broco Jul 01 '14 at 14:40
  • 1
    Delete *wordtemp*, use *word*. – Hans Passant Jul 01 '14 at 14:42
  • Omg I'm so stupid, you're right. It works fine. – Broco Jul 01 '14 at 14:59

0 Answers0