View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Steve Yandl Steve Yandl is offline
external usenet poster
 
Posts: 284
Default Closing a program

The two subroutines below are examples of one approach. These will not work
on Win95/98 systems unless they've added WMI but they should work fine with
WinXP or Win2K.

The subroutine "RunOneNotepad" checks the list of running processes and
determines if any are named notepad.exe (I don't have kobra.exe so I tested
with notepad.exe which I do have). If none are found, it launches a new
process and documents the process ID ("PID") in cell A1 of Sheet1.

The subroutine "KillSpecificPID" extracts the PID from cell A1 (if there is
one there) and kills the process with that ID if it is still running.

___________________________________

Sub RunOneNotepad()
Dim runningNotepad As Boolean

runningNotepad = False

Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cim v2")
Set colProcess = objWMI.ExecQuery _
("Select * from Win32_Process")

For Each objProcess In colProcess
If objProcess.Name = "NOTEPAD.EXE" Then
runningNotepad = True
End If
Next

If runningNotepad = False Then
Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2:W in32_Process")
objWMI.Create "notepad.exe", Null, Null, intProcessID
Sheets(1).Cells(1, 1).Value = intProcessID
End If

runningNotepad = False
Set objWMI = Nothing

End Sub



Sub KillSpecificPID()

intPID = Sheets(1).Cells(1, 1).Value

Set objWMI = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cim v2")
Set colProcess = objWMI.ExecQuery _
("Select * from Win32_Process Where ProcessID = " & intPID & "")
For Each objProcess In colProcess
objProcess.Terminate
Next
End Sub



Steve




"MarkS" wrote in message
...
Hi,
I've never done this or seen anything on it, but i works it's simple
1. Test if program is runing
2. If not start program
3. at end of job shut program down

I can start the program using this
Shell ("C:\Program Files\Reuters\kobra\Program\kobra.exe")
But if its already running it start a second instantance and I don't know
how to get it to shut down

Thanks