Kill Prcoess By Name without any Popup

While developing scripts several times i have noticed that when trying to close the objects due to unforseen error a popup comes up which asks for the confirmation of closing the process or killing the process.
Many a time we require to cleanup the environment by killing all excel & iexplore process before starting the execution. I use the following function to do the same with out giving any popup.

'Process Name= Name of the process from Task manager : Example: Excel.exe

Call KillProcessByName ("Excel.exe")  ' this call with kill the opened excel files irrespective of there status

Public Function KillProcessByName(ProcessName)

Dim objWMIService,colProcessList,objProcess ' Define variables

Set objWMIService =GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")

Set colProcessList =objWMIService.ExecQuery ("Select " & "* from Win32_Process Where Name =" &"'" &ProcessName & "'")     ' Select the process by name

For Each objProcess in colProcessList
     On error resume next
     objProcess.Terminate()   ' Terminate the process
     On error goto 0
Next

'Release the objects
Set objWMIService =Nothing
Set colProcessList =Nothing

End Function

Kindle Wireless Reading Device, Wi-Fi, Graphite, 6" Display with New E Ink Pearl Technology

Comments

Popular posts from this blog

Arrays

What is the difference between eval, execute and executeglobal.

Sample Test Strategy for a MicroService Project with APIs only