Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 读取工作簿.VBProject.References时出现内存不足异常_Vb.net_Excel 2013 - Fatal编程技术网

Vb.net 读取工作簿.VBProject.References时出现内存不足异常

Vb.net 读取工作簿.VBProject.References时出现内存不足异常,vb.net,excel-2013,Vb.net,Excel 2013,我们有一个用VB.NET编写的应用程序,它将两个XLSM工作簿合并成一个XLSM工作簿。 方法是,我们同时打开源工作簿和目标工作簿,获取源工作簿的每一页,并将其插入目标工作簿。这是为所有需要合并到目标工作簿中的工作簿完成的。 合并完成后,我们通过编程将自定义外接程序(.XLA)的引用添加到目标工作簿。 在我们尝试插入自定义加载项(.XLA)的引用之前,一切都进展顺利。要添加外接程序,我们首先检查该外接程序是否已在目标工作簿(业务规则的一部分)中可用。因此,我们迭代每个引用,并将名称与外接程序名称

我们有一个用VB.NET编写的应用程序,它将两个XLSM工作簿合并成一个XLSM工作簿。 方法是,我们同时打开源工作簿和目标工作簿,获取源工作簿的每一页,并将其插入目标工作簿。这是为所有需要合并到目标工作簿中的工作簿完成的。 合并完成后,我们通过编程将自定义外接程序(.XLA)的引用添加到目标工作簿。 在我们尝试插入自定义加载项(.XLA)的引用之前,一切都进展顺利。要添加外接程序,我们首先检查该外接程序是否已在目标工作簿(业务规则的一部分)中可用。因此,我们迭代每个引用,并将名称与外接程序名称进行比较。当我们试图获取excel的引用时,它抛出内存不足异常,即使我们没有外接程序引用,并且只是开箱即用的excel引用(其计数仅为4),也会发生这种情况。 我在代码的下面一点收到异常。 工作簿.VBProject.References 如果源工作簿和目标工作簿都有.xls(93-2007)格式,并且源XLSM工作簿不包含任何宏,那么这种方法可以很好地工作。 有什么问题吗? 另外,在上述应用程序中,所有excel操作都使用后期绑定

Public Function CopySheets(ByVal ExcelWorkbooks As Object, ByVal SourceTemplatePath As String, ByVal TargetTemplatePath As String, ByVal CopyAfter As Boolean, ByVal DeleteOriginalSheets As Boolean) As Integer

    Dim SheetsToBeCopied As Integer = 0
    Dim SourceExcelWorkbook As Object = Nothing
    Dim TargetExcelWorkbook As Object = Nothing
    Dim CopyWorkSheet As Object = Nothing
    Dim SheetBefore As Object = Nothing

    Try

        SourceExcelWorkbook = ExcelWorkbooks.Open(SourceTemplatePath)
        TargetExcelWorkbook = ExcelWorkbooks.Open(TargetTemplatePath)

        Dim lintSheetsToBeCopiedCount As Integer = SourceExcelWorkbook.worksheets.count
        SheetsToBeCopied = lintSheetsToBeCopiedCount
        If CopyAfter = False Then

            While lintSheetsToBeCopiedCount > 0
                SheetBefore = TargetExcelWorkbook.worksheets.item(1)
                CopyWorkSheet = SourceExcelWorkbook.worksheets.item(lintSheetsToBeCopiedCount)
                CopyWorkSheet.move(Before:=SheetBefore)
                lintSheetsToBeCopiedCount = lintSheetsToBeCopiedCount - 1
                TargetExcelWorkbook.save()
            End While

        Else
            Dim lintOriginalSheetCount As Integer = TargetExcelWorkbook.worksheets.count
            Dim lintSheetCount As Integer = lintOriginalSheetCount
            While lintSheetsToBeCopiedCount > 0

                Dim lobjAfterSheet As Object = TargetExcelWorkbook.worksheets.item(lintSheetCount)
                CopyWorkSheet = SourceExcelWorkbook.worksheets.item(1)
                CopyWorkSheet.Move(After:=lobjAfterSheet)
                lintSheetCount = lintSheetCount + 1
                TargetExcelWorkbook.save()
                lintSheetsToBeCopiedCount = lintSheetsToBeCopiedCount - 1

            End While

            TargetExcelWorkbook.save()

            If DeleteOriginalSheets = True Then
                For lintIndex As Integer = 1 To lintOriginalSheetCount
                    TargetExcelWorkbook.worksheets.item(1).delete()
                Next
            End If
        End If

        TargetExcelWorkbook.save()

    Catch ex As Exception
        Throw
    Finally


        If Not CopyWorkSheet Is Nothing Then
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(CopyWorkSheet)
            CopyWorkSheet = Nothing
        End If

        If SheetBefore IsNot Nothing Then
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(SheetBefore)
            SheetBefore = Nothing
        End If

        If Not SourceExcelWorkbook Is Nothing Then
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(SourceExcelWorkbook)
            SourceExcelWorkbook = Nothing
        End If

        If Not TargetExcelWorkbook Is Nothing Then
            Try
                TargetExcelWorkbook.Close(SaveChanges:=False)
            Catch ex As Exception
                'ignore
            End Try
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(TargetExcelWorkbook)
            SourceExcelWorkbook = Nothing
        End If
        GC.Collect()
        GC.WaitForPendingFinalizers()
        GC.Collect()
    End Try
    Return SheetsToBeCopied
End Function

你能提供一些吗code@Bauss-我已经编辑了文章以包含示例代码。你能帮我吗?有人能帮我吗?