Hello everybody. Can someone have a look at this bit of code?
Private Sub TestExcel()
'Create the Excel Application
Dim xlExcel As Application = New Excel.Applicati on
xlExcel.Display Alerts = False
Dim xlWorkbooks As Workbooks = xlExcel.Workboo ks
Try
Dim xlWorkbook As Workbook = xlWorkbooks.Ope n("D:\Test.xls" )
Try
Dim xlWorksheets As Sheets = CType(xlWorkboo k.Worksheets,
Sheets)
Dim xlWorksheet As Worksheet
For Each xlWorksheet In xlWorksheets
xlWorksheet = Nothing
Next
xlWorksheets = Nothing
Finally
xlWorkbook.Clos e()
xlWorkbook = Nothing
End Try
Finally
xlWorkbooks = Nothing
xlExcel.Quit()
xlExcel = Nothing
GC.Collect()
End Try
End Sub
....You will get an error from excel saying "Memory could not be read".
If you change the "For Each" and put a standard For iX = 1 To
xlWorksheets.Co unt the problem goes away. Any explanation? I think
is the way For Each gets the enumerator, but I don't know. I need to
use automation a lot, and I would like to know what is the real
behaviuor of excel in this case.
Thanks a lot.
Arturo