So I have a little gui that will read a given spreadsheet and list all the "valid" sheets. This happens when a user presses a button.
here is my code:
VB.NET Code:
Private Sub bttnScanWorksheets_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnScanWorksheets.Click If System.IO.File.Exists(TextBox1.Text) = True Then 'Clear checkbox area CheckedListBox1.Items.Clear() ' Open excel Dim excelapp As Excel.Application Dim WB As Excel.Workbook Dim WS As Excel.Worksheet excelapp = New Excel.Application WB = excelapp.Workbooks.Open(TextBox1.Text) Dim CheckValid As Boolean Dim SheetsAdded As Boolean = False ' Cycle through each sheet For Each WS In WB.Worksheets Try CheckValid = True If WS.Range("B4").Value.ToString <> "Employee Name:" Then CheckValid = False End If If WS.Range("H4").Value.ToString <> "Week No.:" Then CheckValid = False End If If WS.Range("F8").Value.ToString <> "Cost" Then CheckValid = False End If ' Add the sheet to the list If CheckValid = True Then CheckedListBox1.Items.Add(WS.Name) ' Populate the checkbox area SheetsAdded = True End If Catch ex As NullReferenceException ' Catch errors that arrise from merged cells. Catch ex As Exception MsgBox(ex.ToString) ' any other errors will be caught here End Try Next If SheetsAdded = False Then MsgBox("There were no valid sheets in this workbook.") End If ' Close Excel WB.Close() excelapp.Quit() Marshal.ReleaseComObject(excelapp) excelapp = Nothing GC.Collect() GC.WaitForPendingFinalizers() GC.Collect() Else MsgBox("That file does not exist") End If End Sub
The problem is that Excel.exe doesn't quit until the program closes for some reason. This causes the spreadsheet to remain read only until the program is closed.
Any ideas why excel isn't closing properly?




Reply With Quote