I have this access application that converts csv files into excel files and it works 99% of the time. Unfortunately, sometimes it locks up on on file and then it crashes the whole application. Of course each instance of it crashing I want to fix but I also don't want the single file crashing to lock up the whole application. I found out that the excel file is getting locked up in a background process and that needs to be closed before the application can continue. So what I am doing is after converting each file and then closing excel before I start to convert the next file, I test if excel is already open. If it is open, it will ask the user if they have excel open and if they say no, it will close the background process.
This is the code I have and it is correctly testing if excel is open but I cannot figure out how to close the excel application after.
Sub CloseExcelIfOpen()
Dim objList As Object, objType As Object, strObj$
strObj = "Excel.exe"
Set objType = GetObject("winmgmts:").ExecQuery("select * from win32_process where name='" & strObj & "'")
If objType.Count > 0 And IsExcelOpen = ExcelState.NotChecked Then
IsExcelOpen = MsgBox("Do you have excel open?", vbYesNo)
End If
End Sub