I am creating an excel file using interop.excel and the process is not closing. This is the code i am trying to use.
Private Sub converToExcel(fileLoc As S
'Get the PID from the wHnd and kill the process.
' open the spreadsheet
ImportFileName = OpenFileDialog1.FileName
excel = New Microsoft.Office.Interop.Excel.ApplicationClass
wBook = excel.Workbooks.Open(ImportFileName)
hWnd = excel.Hwnd
Dim id As Integer = GetWindowThreadProcessId(hWnd, ExcelPID)
Sub CloseExcelFile()
Try
' first try this
wBook.Saved = True
wBook.Close()
excel.Quit()
' then this.
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel)
excel = Nothing
' This appears to be the only way to close excel!
Dim oProcess As Process
oProcess = Process.GetProcessById(ExcelPID)
If oProcess IsNot Nothing Then
oProcess.Kill()
End If
Catch ex As Exception
excel = Nothing
Finally
GC.Collect()
End Try
End Sub