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() '--------------------------------------------------------------------------------------- ' : Terminates a process. First checking to see if it is running or not. ' : Uses WMI (Windows Management Instrumentation) to query all running processes ' : then terminates ALL instances of the specified process ' : held in the variable strTerminateThis. ' : ' : ***WARNING: This will terminate a specified running process,use with caution!. ' : ***Terminating certain processes can effect the running of Windows and/or ' : ***running applications. '--------------------------------------------------------------------------------------- Dim strTerminateThis As String 'The variable to hold the process to terminate Dim objWMIcimv2 As Object Dim objProcess As Object Dim objList As Object Dim intError As Integer strTerminateThis = "notepad.exe" 'Process to terminate, 'change notepad.exe to the process you want to terminate Set objWMIcimv2 = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\.\root\cimv2") 'Connect to CIMV2 Namespace Set objList = objWMIcimv2.ExecQuery _ ("select * from win32_process where name='" & strTerminateThis & "'") 'Find the process to terminate If objList.Count = 0 Then 'If 0 then process isn't running 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 'Ask if OK to continue 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 'OK to continue with terminating the process For Each objProcess In objList intError = objProcess.Terminate 'Terminates a process and all of its threads. 'Return value is 0 for success. Any other number is an error. If intError <> 0 Then MsgBox "ERROR: Unable to terminate that process.", vbCritical, "Aborting" Exit Sub End If Next 'ALL instances of specified process (strTerminateThis) has been terminated 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 'NOT OK to continue with the termination, abort Set objWMIcimv2 = Nothing Set objList = Nothing Set objProcess = Nothing Exit Sub End Select End If End Sub

How to use:

  1. Copy the procedure 'TerminateProcess' displayed above.
  2. Open Excel.
  3. Goto Tools then Macro then Visual Basic Editor.
  4. In the VBE select Insert then Module.
  5. Paste the procedure to the module you've just created.
  6. Change "notepad.exe" held in strTerminateThis to whatever process you want to terminate.
  7. Close the VBE. (File then Close and Return to Microsoft Excel).
  8. Run the code. One way to do this is:
  9. Selecting Tools then Macro then Macros from Excel's main menu.
  10. Highlight TerminateProcess.
  11. Click the Run button.
  12. If you OK the message to terminate process:
  13. ALL running instances of the process will now close WITHOUT asking to save any changes.
 

Test the code:

  1. Open Windows Notepad.
  2. Copy the sub 'TerminateProcess' displayed above.
  3. Open Excel.
  4. Goto Tools then Macro then Visual Basic Editor.
  5. In the VBE select Insert then Module.
  6. Paste the procedure to the module you've just created.
  7. Close the VBE. (File then Close and Return to Microsoft Excel).
  8. Run the code. One way to do this is:
  9. Selecting Tools then Macro then Macros from Excel's main menu.
  10. Highlight TerminateProcess.
  11. Click the Run button.
  12. If you OK the message to terminate process:
  13. 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.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express