Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

Try With Excel

$
0
0
Apparently I don't understand the Try/Catch As Exception as much as I thought I did. I work with Excel files a lot and making sure that Excel gets shutdown when the code is through is important. What I'm finding equally important is to shutdown Excel in the event of an error. So here is my code to try and do this:

Code:

Imports System
Imports System.IO
Imports Microsoft.Office
Imports Microsoft.Office.Interop
Imports System.Runtime.InteropServices

Module Module1

    Sub Main()

        Dim vExcelApp As Excel.Application = Nothing
        Dim vExcelWorkbooks As Excel.Workbooks = Nothing
        Dim vExcelWorkbook As Excel.Workbook = Nothing
        Dim vExcelSheet1 As Excel.Worksheet = Nothing
        Dim vExcelSheet2 As Excel.Worksheet = Nothing
        Dim vExcelRange1 As Excel.Range = Nothing
        Dim NovellPath As String = "C:\My_Path\"
        Dim ExcelFile As String = NovellPath & "My_Excel_File.xls"

        Try

            '************************************
            ' Open Excel and Populate Cells with data
            '************************************
            vExcelApp = New Excel.Application
            vExcelApp.DisplayAlerts = False
            vExcelApp.Visible = False
            vExcelWorkbooks = vExcelApp.Workbooks
            vExcelWorkbook = vExcelApp.Workbooks.Open(ExcelFile)
            vExcelSheet1 = CType(vExcelWorkbook.Sheets("Sheet1"), Excel.Worksheet)

            ************************************************
            Do a bunch of stuff in Excel, populate cells, etc...
          ************************************************

        Catch ex As Exception

        Finally

            '***************************
            ' NOW SHUT DOWN EXCEL
            '***************************
            vExcelApp.DisplayAlerts = False
            vExcelWorkbook.Close(True)
            vExcelApp.UserControl = True
            vExcelApp.Quit()

            Marshal.FinalReleaseComObject(vExcelRange1)
            Marshal.FinalReleaseComObject(vExcelSheet1)
            Marshal.FinalReleaseComObject(vExcelWorkbook)
            Marshal.FinalReleaseComObject(vExcelWorkbooks)
            Marshal.FinalReleaseComObject(vExcelApp)

            vExcelRange1 = Nothing
            vExcelApp = Nothing
            vExcelWorkbooks = Nothing
            vExcelWorkbook = Nothing
            vExcelSheet1 = Nothing
            vExcelSheet2 = Nothing

            GC.Collect()
            GC.WaitForPendingFinalizers()
            GC.Collect()
            GC.WaitForPendingFinalizers()

        End Try

    End Sub

End Module

Ok so I started debug and it got to a place in the code where it encountered the lock for editing error which I knew would happen. But the program then came to a screeching hault on the first Marshal.Final.Release command saying the value cannot be null. And thus I'm left with an instance of Excel hanging in the task manager.

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>