1

Before this question gets closed because its a duplicate, I tried these answers.

Excel Process not closing in VB.net

Application not quitting after calling quit

VB.NET Excel Program Leaves EXCEL.EXE floating after completion

Excel process still runs after closing in VB.net

But none of them worked for me, I also tried looking from the web.

Opening an excel file leaves "EXCEL.EXE" process open (and plenty of others) But the process still remains.

This software will be a automatic uploader that will run when the computer boots up. then it will check the Drive D partition of the computer and will upload all excel in it. I have already used this to upload CSV files and never had such problems, however, now I am trying to upload Excel files.

I want to know how can I properly kill the process without using the "KILL" command. I also tried to use process.kill but when I open another excel workbook from the computer, that excel gets killed, not the excel being uploaded.

Do take note that this code is somehow modified to simply output the contents of the excel, it works already, I just want to know how to close the EXCEL PROCESS since there might be a time that this system will upload hundreds of excel worksheets.

I used this code below:

 Sub uploadExcelFiles(ByVal locs As String)

            Dim xlApp As Excel.Application
            Dim xlWorkBook As Excel.Workbook
            Dim xlWorkSheet As Excel.Worksheet

            Dim location As String
            Dim isData As Boolean = False
            Dim m_CountTo As Integer = 0

            filename = ""
            globalloc = ""
            'Gets Files from Folders
            Dim counting As Integer = 0
            Dim csvcount As String() = Directory.GetFiles(locs, "*.xls")
            Dim ToUploadCSV As String

            ExcelProcessInit()


            For counting = 0 To csvcount.Count - 1
                CheckListofCSV.Clear()
                filename = ""

                location = csvcount(counting).ToString
                globalloc = location

                ToUploadCSV = Path.GetFileName(location).ToString
                Dim ListLines As New List(Of String)
                Dim ListLinesNEW As New List(Of String)

                xlApp = New Excel.Application
                xlWorkBook = xlApp.Workbooks.Open(location)
                xlWorkSheet = xlWorkBook.Worksheets("Sheet1")
                'Use this code to read the data from EXCEL

                '**DATA HEADER**
                MsgBox(xlWorkSheet.Cells(2, 6).value)
                MsgBox(xlWorkSheet.Cells(2, 7).value)
                MsgBox(xlWorkSheet.Cells(2, 8).value)
                MsgBox(xlWorkSheet.Cells(3, 2).value)
                MsgBox(xlWorkSheet.Cells(3, 5).value)
                MsgBox(xlWorkSheet.Cells(3, 8).value)
                MsgBox(xlWorkSheet.Cells(4, 2).value)
                MsgBox(xlWorkSheet.Cells(4, 5).value)
                MsgBox(xlWorkSheet.Cells(4, 7).value)
                MsgBox(xlWorkSheet.Cells(4, 8).value)
                MsgBox(xlWorkSheet.Cells(62, 2).value)
                '**DATA HEADER**

                '**DATA DETAILS**
                'ROWS
                Dim i As Integer = 5
                'COLUMNS
                Dim col As Integer = 2
                'SAMPLING COUNT FROM EXCEL
                Dim SampQTYXcel As Integer = 0

                Do While Len(xlWorkSheet.Cells(i, col).value) > 0


                    For i = 5 To 10
                        MsgBox(xlWorkSheet.Cells(i, col).value)
                    Next
                    'This is sampling QTY
                    getSamplingQty(xlWorkSheet.Cells(3, 2).value, xlWorkSheet.Cells(5, col).value, xlWorkSheet.Cells(3, 8).value)

                    'Sample Data 10 onwards
                    Do While Len(xlWorkSheet.Cells(i, col).value) > 0
                        MsgBox(xlWorkSheet.Cells(i, col).value)
                        i = i + 1
                        SampQTYXcel = SampQTYXcel + 1
                    Loop

                    'Excel data has more data than Database QTY then REPORT 
                    If SampQTYXcel > SamplQTY Then
                        'SEND ERROR
                        MsgBox("ERROR")
                    End If

                    'Add 1 to move to next column
                    col = col + 1
                    i = 5
                    SampQTYXcel = 0
                Loop

                xlWorkBook.Save()

                '**DATA DETAILS**

                xlApp.DisplayAlerts = False
                xlWorkBook.Close()
                xlApp.Quit()

                xlApp = Nothing
                xlWorkBook = Nothing
                releaseObject(xlWorkBook)
                releaseObject(xlWorkSheet)
                releaseObject(xlApp)

                GC.Collect()
                GC.WaitForPendingFinalizers()

                ExcelProcessKill()

            Next

        End Sub

EDIT:

I forgot to add the codes of ExcelProcessInit() and ExcelProcessKill()

Here are their codes:

Private Sub ExcelProcessInit()
    Try
        'Get all currently running process Ids for Excel applications
        mExcelProcesses = Process.GetProcessesByName("EXCEL")
    Catch ex As Exception
    End Try
End Sub

 Private Sub ExcelProcessKill()
        Dim oProcesses() As Process
        Dim bFound As Boolean

        Try
            'Get all currently running process Ids for Excel applications
            oProcesses = Process.GetProcessesByName("EXCEL")

            If oProcesses.Length > 0 Then
                For i As Integer = 0 To oProcesses.Length - 1
                    bFound = False

                    For j As Integer = 0 To mExcelProcesses.Length - 1
                        If oProcesses(i).Id = mExcelProcesses(j).Id Then
                            bFound = True
                            Exit For
                        End If
                    Next

                    If Not bFound Then
                        oProcesses(i).Kill()
                    End If
                Next
            End If
        Catch ex As Exception
        End Try
    End Sub
Mr.J
  • 430
  • 2
  • 10
  • 30
  • When you say none of these worked for me.. what, briefly, happened in each case? – QHarr Jul 05 '18 at 07:22
  • @QHarr, After trying everything that I have read from SO, I launch the app, then check through Windows Task Manager if the `EXCEL` process got removed, but it did not. Then using `PROCESS.KILL` It did remove the `EXCEL` Process but the user does not want a tool that closes his work suddenly. Thank you for your time. – Mr.J Jul 05 '18 at 07:25
  • 1
    I believe it comes down to the order in which you release them. Have a look at my answer [here](https://stackoverflow.com/questions/44438271/count-lines-max-with-values/44453387#44453387) which links to another well put together answer. These may help. Remember to close things down in the reverse order. So close the sheet, then the workbook, then the application. – Bugs Jul 05 '18 at 09:05
  • Also noted in your profile that you talk about writing good questions. This question, IMO is good, but for reference have a read of [this](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) which goes into detail :) – Bugs Jul 05 '18 at 09:11
  • Unfortunately , you have stumbled into the dark-side of SO, where cult programming solutions are worshiped instead walking in the light of knowledge. Hans wrote a [very good answer](https://stackoverflow.com/a/25135685/2592875) on why the COM objects as not released and how to solve the problem. A variation of the technique he presented that uses [Marshal.AreComObjectsAvailableForCleanup] is [presented here](https://stackoverflow.com/a/36578663/2592875). – TnTinMn Jul 05 '18 at 14:48
  • @Bugs your answer helped me, I flagged this question as duplicate to that question because that is the answer I am looking for. – Mr.J Jul 06 '18 at 00:30
  • 1
    Not a problem @Mr.J glad it helped. – Bugs Jul 06 '18 at 06:13

0 Answers0