RPC-E-SYS-CALL-FAILED上下文断开连接

时间:2014-07-01 13:30:50

标签: vb.net office-interop

我的VB.net程序有问题(Visual Studio 2010)。 它创建一个新的Word-File,打开其他Word文件(在用户可以选择的表单上),选择并复制他们的内容并将其粘贴到新文件中,所以基本上你有一个包含所有其他内容的文件

使用一定数量的文件可以很好地运行。 但是,如果用户选择所有这些(121),程序最终会在~3分钟后因RPC-E-SYS-CALL-FAILED异常而崩溃。 我试图谷歌它,我已经知道它必须与Threads或MessageQueue做一些事情,但我对VB.Net很新,并希望有一个新手解释发生了什么,为什么它'正在发生以及我必须做些什么来优化我的代码。

相关代码:

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

0 个答案:

没有答案