Question : Excel wont close - Still runs in task manager (vb.net)

I Have the following code below which does not seem to kill the excel objects after they have been created.
I have tried the GC as well as the  System.Runtime.InteropServices.Marshal.ReleaseComObject(Reference).

Thanks
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
Public Function NameOfExcelSheet(ByVal strFileToOpen as String) as String
        Dim arrSheet(0) as String
        Dim i as Integer = 0
     
        objExcel = CreateObject("EXCEL.APPLICATION")
        objWorkBook = objExcel.Workbooks.Open(strFileToOpen)
        
        For Each objSheet In objWorkBook.Worksheets
            'Me.ListBox1.Items.Add(tworksheet.Name)
            'msgbox(objSheet.Name)
            Array.Resize(arrSheet,i + 1)
            arrSheet(i) = objSheet.Name
            i = i + 1
        Next
        return arrSheet(0) ' Only need the first element
        
        objWorkBook.Close
        objExcel.quit ' Quit Excel
        '*************************************************** 
        'Release objects
        objWorkBook = Nothing
        objExcel = Nothing
        '*************************************************** 
        
     '   
     '   objWorkBook.Close
     '   objExcel.quit ' Quit Excel
     '   objExcel = Nothing
     '   objWorkBook = Nothing
     '   objSheet = Nothing
     '   GC.Collect()
     '   GC.WaitForPendingFinalizers()
     '   ReleaseComObject(objExcel)
     '   ReleaseComObject(objWorkBook)
     '   ReleaseComObject(objSheet)
    End Function

Answer : Excel wont close - Still runs in task manager (vb.net)

The only way to identify a unique excel process would be to assign its window a name (a caption) - and then retrieve the handle to that window. The handle is one step away from the processId - which can then be used to kill the specific process.

You will need to set the caption for the application as follows
' set your application's window caption
App.Caption = System.Guid.NewGuid.ToString.ToUpper

Then you can call the EnsureProcessKilled function passing in the arguments (nothing, App.Caption) - e.g. EnsureProcessKilled(nothing, App.Caption)

Try that and see if it works.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
' set your application's window caption
App.Caption = System.Guid.NewGuid.ToString.ToUpper

Declare Function EndTask Lib "user32.dll" (ByVal hWnd As IntPtr) As Integer
    Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" _
           (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Declare Function GetWindowThreadProcessId Lib "user32.dll" _
           (ByVal hWnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
    Declare Function SetLastError Lib "kernel32.dll" (ByVal dwErrCode As Integer) As IntPtr

Public Sub EnsureProcessKilled(ByVal MainWindowHandle As IntPtr, ByVal Caption As String)
        SetLastError(0)
        ' for Excel versions <10, this won't be set yet
        If IntPtr.Equals(MainWindowHandle, IntPtr.Zero) Then _
      MainWindowHandle = FindWindow(Nothing, Caption)
        If IntPtr.Equals(MainWindowHandle, IntPtr.Zero) Then _
      Exit Sub ' at this point, presume the window has been closed.
        Dim iRes, iProcID As Integer
        iRes = GetWindowThreadProcessId(MainWindowHandle, iProcID)
        If iProcID = 0 Then ' can’t get Process ID
            If EndTask(MainWindowHandle) <> 0 Then Exit Sub ' success
            Throw New ApplicationException("Failed to close.")
        End If
        Dim proc As System.Diagnostics.Process
        proc = System.Diagnostics.Process.GetProcessById(ProcessID)
        proc.CloseMainWindow()
        proc.Refresh()
        If proc.HasExited Then Exit Sub
        proc.Kill()
    End Sub
Random Solutions  
 
programming4us programming4us