In VB.NET I'm reading an Excel Spreadsheet like so:
Dim xlApp As Excel.Application = New Excel.Application
Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open(Server.MapPath(SavePath & sFilename))
Dim xlWorkSheet As Excel.Worksheet = xlWorkBook.Sheets(1)
Dim eRange As Excel.Range = xlWorkSheet.Range("C3:C" & xlApp.Rows.End(Excel.XlDirection.xlDown).Row)
Dim bottomRange As Integer = xlApp.Rows.End(Excel.XlDirection.xlDown).Row
...
After I open it up and read some data I want to close it and make it possible for someone to delete it MANUALLY(clicking on the file and pressing delete) afterwards, so after looking around I found this sub that is supposed to release the locks on the objects that I create:
Private Sub ReleaseObject(ByVal obj As Object)
Try
Dim intRel As Integer = 0
Do
intRel =
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj)
Loop While intRel > 0
Catch ex As Exception
MsgBox("Error releasing object" & ex.ToString)
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
But whenever I create a spreadsheet and close my program and then try to delete the file MANUALLY, not in my program, I get the error:
The action can't be completed because the file is open in Excel 2016. Choose the file and try again.
This error even persists after I have closed my program. Can someone please help me out in figuring out how to release these locks Excel has on the files? I should also note that I pass xlApp to the sub.