|
|
|
|
|
|
Excel
|
Terminate a running process.
|
|
Ease of Use
|
Intermediate
|
Version tested with
|
2000, 2002
|
Submitted by:
|
Marcster
|
Description:
|
Terminates ALL running instances of a specified process by using WMI (Windows Management Instrumentation).
For example: If 'notepad.exe' was the specified process (held in variable strTerminateThis), the code first checks to see if it's
running, then asks wheather or not to continue with terminating ALL running instances of 'notepad.exe'. If OK to continue, the code will terminate ALL running instances of 'notepad.exe'
|
Discussion:
|
This KB entry has been tested on Excel 2000 on Windows XP Professional and also on Excel 2002 on Windows XP Home Edition.
The code below will terminate, a specific running process. (To view all currently running processes on your PC, select 'Task Manager' from right-clicking on your Windows taskbar. Then click on the Processes tab.)
The following code needs a process in which to terminate. This is held in the variable strTerminateThis. The code below will first check to see if the specified process (in the example below this is notepad.exe) is running and if it is, will ask if OK to proceed. If OK the code will terminate ALL instances of it. The code uses WMI (Windows Management Instrumentation) to do this, so no API (Application Programming Interface) is used.
***WARNING***:
The code below terminates ALL instances of a specified running process, use with caution!. Terminating certain processes can effect the running of Windows and/or applications. The process will terminate if you OK it, WITHOUT giving you the chance to save any changes in anything that is running in the specified process.
For example, if you typed something into notepad and ran the code 'TerminateProcess', and clicked OK to the message asking to continue, then ALL instances of notepad will close and any data held in it will be lost!.
|
Code:
|
instructions for use
|
Option Explicit
Sub TerminateProcess()
Dim strTerminateThis As String
Dim objWMIcimv2 As Object
Dim objProcess As Object
Dim objList As Object
Dim intError As Integer
strTerminateThis = "notepad.exe"
Set objWMIcimv2 = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objList = objWMIcimv2.ExecQuery _
("select * from win32_process where name='" & strTerminateThis & "'")
If objList.Count = 0 Then
MsgBox strTerminateThis & " is NOT running." & vbCr & vbCr _
& "Exiting procedure.", vbCritical, "Unable to continue"
Set objWMIcimv2 = Nothing
Set objList = Nothing
Set objProcess = Nothing
Exit Sub
Else
Select Case MsgBox("Are you sure you want to terminate this running process?:" _
& vbCrLf & "" _
& vbCrLf & "Process name: " & strTerminateThis _
& vbCrLf & "" _
& vbCrLf & "Note:" _
& vbCrLf & "Terminating certain processes can effect the running of Windows" _
& "and/or running applications. The process will terminate if you OK it, WITHOUT " _
& "giving you the chance to save any changes in anything that is running in the specified process above." _
, vbOKCancel Or vbQuestion Or vbSystemModal Or vbDefaultButton1, "WARNING:")
Case vbOK
For Each objProcess In objList
intError = objProcess.Terminate
If intError <> 0 Then
MsgBox "ERROR: Unable to terminate that process.", vbCritical, "Aborting"
Exit Sub
End If
Next
Call MsgBox("ALL instances of process " & strTerminateThis & " has been successfully terminated.", _
vbInformation, "Process Terminated")
Set objWMIcimv2 = Nothing
Set objList = Nothing
Set objProcess = Nothing
Exit Sub
Case vbCancel
Set objWMIcimv2 = Nothing
Set objList = Nothing
Set objProcess = Nothing
Exit Sub
End Select
End If
End Sub
|
How to use:
|
- Copy the procedure 'TerminateProcess' displayed above.
- Open Excel.
- Goto Tools then Macro then Visual Basic Editor.
- In the VBE select Insert then Module.
- Paste the procedure to the module you've just created.
- Change "notepad.exe" held in strTerminateThis to whatever process you want to terminate.
- Close the VBE. (File then Close and Return to Microsoft Excel).
- Run the code. One way to do this is:
- Selecting Tools then Macro then Macros from Excel's main menu.
- Highlight TerminateProcess.
- Click the Run button.
- If you OK the message to terminate process:
- ALL running instances of the process will now close WITHOUT asking to save any changes.
|
Test the code:
|
- Open Windows Notepad.
- Copy the sub 'TerminateProcess' displayed above.
- Open Excel.
- Goto Tools then Macro then Visual Basic Editor.
- In the VBE select Insert then Module.
- Paste the procedure to the module you've just created.
- Close the VBE. (File then Close and Return to Microsoft Excel).
- Run the code. One way to do this is:
- Selecting Tools then Macro then Macros from Excel's main menu.
- Highlight TerminateProcess.
- Click the Run button.
- If you OK the message to terminate process:
- Code will close ALL instances of notepad.exe WITHOUT asking to save any changes.
|
Sample File:
|
TerminateProcess.zip 10.21KB
|
Approved by mdmackillop
|
This entry has been viewed 332 times.
|
|