R
Rob Oldfield
This seems to be a topic that has been discussed at length in newsgroups,
but I still can't get it to behave...
I'm using automation to get at an existing Excel spreadsheet and then
retrieve data from it. I then want to close down the Excel process. What
I'm getting though, is the process remaining in memory until my app shuts
down.
I am, by the way, aware that MS article 257819 is a better method of getting
at the data, but sadly the spreadsheets that I'm working with are created by
another piece of software and they're just not quite right. The ADO
approach doesn't work with them.
My code (referencing MS article 317109 and numerous newsgroup postings) -
just a sample to simply open Excel and the file, and then to shut it
down....the form will need a reference to the Excel object library.....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Open the file
Dim file As String = "c:\data\vs\test.xls"
Cursor.Current = Cursors.WaitCursor
Dim objExcel As Excel.Application
Dim Books As Excel.Workbooks
Dim Book As Excel.Workbook
Dim Sheet As Excel.Worksheet
objExcel = New Excel.Application()
Books = objExcel.Workbooks
Book = Books.Open(file)
Sheet = Book.Worksheets(1)
Sheet.Unprotect()
'...and then attempt to close it
Dim objEachWorkSheet As Excel.Worksheet
Dim objEachWorkBook As Excel.Workbook
For Each objEachWorkBook In objExcel.Workbooks
For Each objEachWorkSheet In objEachWorkBook.Worksheets
NAR(CObj(objEachWorkSheet))
Next
objEachWorkBook.Close(False)
NAR(CObj(objEachWorkBook))
Next
NAR(objExcel.Workbooks)
objExcel.Quit()
NAR(CObj(objExcel))
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
Cursor.Current = Cursors.Default
End Sub
Private Sub NAR(ByVal o As Object)
Try
Dim i As Integer
Do
i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Loop While i > 0
Catch excpUnexpected As Exception
MsgBox(excpUnexpected.ToString())
Finally
o = Nothing
End Try
End Sub
Anyone have a method of putting the Excel process out of it's misery?
but I still can't get it to behave...
I'm using automation to get at an existing Excel spreadsheet and then
retrieve data from it. I then want to close down the Excel process. What
I'm getting though, is the process remaining in memory until my app shuts
down.
I am, by the way, aware that MS article 257819 is a better method of getting
at the data, but sadly the spreadsheets that I'm working with are created by
another piece of software and they're just not quite right. The ADO
approach doesn't work with them.
My code (referencing MS article 317109 and numerous newsgroup postings) -
just a sample to simply open Excel and the file, and then to shut it
down....the form will need a reference to the Excel object library.....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Open the file
Dim file As String = "c:\data\vs\test.xls"
Cursor.Current = Cursors.WaitCursor
Dim objExcel As Excel.Application
Dim Books As Excel.Workbooks
Dim Book As Excel.Workbook
Dim Sheet As Excel.Worksheet
objExcel = New Excel.Application()
Books = objExcel.Workbooks
Book = Books.Open(file)
Sheet = Book.Worksheets(1)
Sheet.Unprotect()
'...and then attempt to close it
Dim objEachWorkSheet As Excel.Worksheet
Dim objEachWorkBook As Excel.Workbook
For Each objEachWorkBook In objExcel.Workbooks
For Each objEachWorkSheet In objEachWorkBook.Worksheets
NAR(CObj(objEachWorkSheet))
Next
objEachWorkBook.Close(False)
NAR(CObj(objEachWorkBook))
Next
NAR(objExcel.Workbooks)
objExcel.Quit()
NAR(CObj(objExcel))
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
Cursor.Current = Cursors.Default
End Sub
Private Sub NAR(ByVal o As Object)
Try
Dim i As Integer
Do
i = System.Runtime.InteropServices.Marshal.ReleaseComObject(o)
Loop While i > 0
Catch excpUnexpected As Exception
MsgBox(excpUnexpected.ToString())
Finally
o = Nothing
End Try
End Sub
Anyone have a method of putting the Excel process out of it's misery?