Reputation: 2276
I created the class below to open and gather the names of worksheets in an excel file. It does as it should in opening the file and returning the names of the each individual worksheet. Unfortunatly, I'm unable to close the file. This keeps the Excel.exe process hanging. Even after I attempt to close it at the bottom of this class the process still hangs. I have to manually goto Windows Task Manager and kill the process before I can use the file again. Even if I exit out of the application it still is out there. Any suggestion on how to kill this process from the code?
Imports Excel = Microsoft.Office.Interop.Excel
Public Class ExcelWrapper
Dim strTypeSelection As String = String.Empty
Dim strFilePath As String = String.Empty
Function GetWorksheetsByName(ByVal strfilePath As String) As Array
Dim xlsApp As Excel.Application
Dim xlsWorkBook As Excel.Workbook
xlsApp = New Excel.Application
xlsWorkBook = xlsApp.Workbooks.Open(strfilePath)
Dim intWsCount As Integer = xlsApp.Worksheets.Count
Dim sarWorksheetName(intWsCount - 1) As String
Dim i As Integer = 0
'gathers the names off all the worksheets
For Each totalWorkSheets In xlsApp.Worksheets
sarWorksheetName(i) = totalWorkSheets.Name
i += 1
Next totalWorkSheets
xlsWorkBook.Close(strfilePath)
xlsApp.DisplayAlerts = False
xlsApp.Quit()
Return sarWorksheetName
End Function
End Class
Upvotes: 4
Views: 12453
Reputation: 27488
I would modify the end part of your code like so, to correct some issues that may be causing your problem:
For Each totalWorkSheets as Excel.Worksheet In xlsWorkBook.Worksheets 'not xlsApp.Worksheets
sarWorksheetName(i) = totalWorkSheets.Name
i += 1
Next totalWorkSheets
xlsWorkBook.Close Savechanges:=False
set xlsWorkBook = Nothing
xlsApp.Quit()
set xlsApp = Nothing
Upvotes: 0
Reputation: 15091
This is not specific to your Excel issue; but you can kill a process by name using...
Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("WhateverYouWant")
For Each p As Process In pProcess
p.Kill()
Next
That will kill all processes that match the particular name.
You might want to try adding Excel = Nothing to see if that clears up the issue (and you can avoid forcefully killing the process'.
Upvotes: 0
Reputation: 192621
From http://www.experts-exchange.com/Programming/Misc/Q_24049269.html
' after creating it, but before operating on it
xlsApp.Application.DisplayAlerts = False
Upvotes: 2
Reputation: 175936
For each reference try adding;
System.Runtime.InteropServices.Marshal.ReleaseComObject(xxx)
xxx = nothing
(http://support.microsoft.com/kb/317109)
Upvotes: 3
Reputation: 9389
Here is cleanup code that I use when working with Excel through Automation. It is essential you clean up properly or the process will hang as you are seeing. I assume you are doing this through a desktop application and not a web app. This is usually done in a finally block and the sample is c# but I think you should be able to adapt it to your needs.
GC.Collect();
GC.WaitForPendingFinalizers();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(range);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(sheet);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(book);
WB.Close(false, Type.Missing, Type.Missing);
Excel.Quit();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(Excel);
Upvotes: 0
Reputation: 29548
I've run into this issue before as well, and as I recall the solution was to call Marshal.ReleaseComObject(Object obj)
or Marshal.FinalReleaseComObject(Object obj)
on all of the objects that the come from the Office libraries. I could very easily be wrong though. Good luck, Office automation is a huge pain.
Upvotes: 4