Microsoft Access can Terminate Processes using WMI. In other words, Access can shut down an application that is running on a computer. This capability is very useful when using Office automation or automation with other applications. For example, you may want to open an Excel Spreadsheet, import data, and then close Excel using the code in this article.
Access VBA can also be used to terminate a program when it “hangs” or “freezes”. I have at times thorougly enjoyed terminiating some repeat offenders using Access or VBSripts.
The code below shows the basics of how we can terminate a Process. We are using the name of the process in the WQL Query so we do not have to loop through all of the running processes. For example, if we want to close Notepad we can call the procedure using the name of the process as an argument like this:
Call TerminateProcess(“notepad.exe”)
Sub TerminateProcess(strProcess As String)
Dim objWMIService As Object
Dim colProcesses As Object
Dim objProcess As Object
Dim strComputer As String
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("SELECT * FROM Win32_Process WHERE" & _
" Name = '" & strProcess & "'")
For Each objProcess In colProcesses
objProcess.Terminate
Next objProcess
Set objProcess = Nothing
Set colProcesses = Nothing
Set objWMIService = Nothing
End Sub
We can make this code more helpful by getting the result of the Terminate Method. We can change the procedure to a function that will return the result as an integer. We can also use a Message Box. The modified code below does both.
Function ProcessTerminate(strProcess As String) As Long
Dim objWMIService As Object
Dim colProcesses As Object
Dim objProcess As Object
Dim strComputer As String
Dim strMsg As String
Dim intReturn As Long
Dim booFound As Boolean
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery _
("SELECT * FROM Win32_Process WHERE" & _
" Name = '" & strProcess & "'")
For Each objProcess In colProcesses
' The Process is running - set the flag to True
booFound = True
' End the Process and get the Return code
intReturn = objProcess.Terminate
Next objProcess
' Start a Message string
strMsg = " Process Name: " & strProcess & " " & vbCrLf & vbCrLf
' Determine the result
Select Case intReturn
Case 0
If booFound = True Then
strMsg = strMsg & " Termination succeeded. "
Else
strMsg = strMsg & " Unknown failure. "
intReturn = 8
End If
Case 2
strMsg = strMsg & " Access denied. "
Case 3
strMsg = strMsg & " Insufficient privilege. "
Case 8
strMsg = strMsg & " Unknown failure. "
Case 9
strMsg = strMsg & " Path not found. "
Case 21
strMsg = strMsg & " Invalid parameter. "
Case Else
strMsg = strMsg & " Termination failed for unknown reason. "
End Select
strMsg = strMsg & vbCrLf & vbCrLf & " Time: " & Now
MsgBox strMsg
ProcessTerminate = intReturn
Set objProcess = Nothing
Set colProcesses = Nothing
Set objWMIService = Nothing
End Function
We can also Terminate more than one process by creating an array of process names.
Sub ProcessesTerminate()
Dim objWMIService As Object
Dim colProcesses As Object
Dim objProcess As Object
Dim strComputer As String
Dim intReturn As Long
Dim strTargetProc
Dim arrTargetProcs
strComputer = "."
' Create the array of process names
arrTargetProcs = Array("mspaint.exe", "calc.exe", "notepad.exe")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\" _
& strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery( _
"SELECT * FROM Win32_Process", , 48)
Debug.Print "Checking for target processes ..."
For Each objProcess In colProcesses
For Each strTargetProc In arrTargetProcs
' Make all process names lowercase so they will match
If LCase(objProcess.Name) = LCase(strTargetProc) Then
Debug.Print vbCrLf & "Process Name: " & objProcess.Name
Debug.Print " Time: " & Now
intReturn = objProcess.Terminate
If intReturn = 0 Then
Debug.Print " Terminated"
Else
Debug.Print " Unable to terminate"
End If
End If
Next
Next
Set objProcess = Nothing
Set colProcesses = Nothing
Set objWMIService = Nothing
End Sub
We will learn more about how VBA can leverage the capabilities of WMI in the articles to come.
Republished from Access Easy Tips [18 clicks].
Read the original version here [32134 clicks].