读取Workbook.VBProject.References

时间:2015-08-28 08:01:42

标签: vb.net excel-2013

我们有一个用VB.NET编写的应用程序,它将两个XLSM工作簿合并到一个单一的XLSM工作簿中。 方法是我们打开源工作簿和目标工作簿,并获取源工作簿的每个工作表并将其插入到目标工作簿中。这适用于需要合并到目标工作簿的所有工作簿。 合并完成后,我们以编程方式将我们的自定义加载项(.XLA)的引用添加到目标工作簿。 一切顺利,直到我们尝试插入我们的自定义插件(.XLA)的引用。要添加插件,我们首先检查插件是否已在目标工作簿中可用(业务规则的一部分)。因此,我们遍历每个引用并将名称与我们的加载项名称进行比较。在我们尝试获取excel的引用时,它会抛出内存异常,即使我们没有添加引用并且只是开箱即用的Excel引用(其计数仅为4),也会发生这种情况。 我在代码中的以下点接收了异常。 Workbook.VBProject.References 如果源和目标工作簿都有.xls(93-2007)格式,并且源XLSM工作簿不包含任何宏,则此方法也可以正常工作。 任何指针可能是什么问题? P.S - 我们在上述申请中对所有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

0 个答案:

没有答案