I have a need to kill an Excel process running in background. I have done it successfully on a machine running 32-bit Office using the code shown below. This code did not execute on a machine running 64-bit Office, and threw an Automation error at the statement Set objProessSet = . . . (although I think the issue is failure of the immediately preceding statement). My OS is Windows 10. So, my question is what changes do I need to make to the two Set statements to allow the code to run with 64-bit Office. Alternately, is there code that will run correctly on both versions of Office? Another possibility is that a completely different approach is needed for 64-bit Office.
Dim objServices As Object, objProcessSet As Object, Process As Object
Dim command As String
Dim cmd As String: cmd = "cmd.exe /S /C taskkill /pid "
Set objServices = GetObject("winmgmts:\\.\root\CIMV2")
Set objProcessSet = objServices.ExecQuery("SELECT ProcessID, FROM Win32_Process WHERE name = ""excel.exe""", , 48)
'loop over all Excel processes - should be only one at most
For Each Process In objProcessSet
command = cmd & Process.ProcessID
Call Shell(command, vbNormalFocus)
Next Process