View Single Post
  #2 (permalink)  
Old September 29th, 2006, 06:44 AM
mmcdonal mmcdonal is offline
Friend of Wrox
Points: 9,611, Level: 42
Points: 9,611, Level: 42 Points: 9,611, Level: 42 Points: 9,611, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2004
Location: Washington, DC, USA.
Posts: 3,069
Thanks: 0
Thanked 10 Times in 10 Posts
Default

You should check for the existence of the process at the beginning of each set of code for using the process, and if it is still running, kill the process first. I am sure that the posters here can show you a more elegant way, but here is the VBScript version that will run in your VBA:

Dim sComputer

sComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel = impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcesses = objWMIService.ExecQuery("SELECT * FROM Win32_Process")
For Each objProcess in colProcesses
    If objProcess.Name = "EXCEL.EXE" Then
           WScript.Echo objProcess.Name
           errReturnCode = objProcess.Terminate()
       End If
Next

I have tested this and it works on a Windows XP box.

HTH

mmcdonal
Reply With Quote