搬家第25天-citectv7.4cittectVBA判断某个程序是否打开,已经打开就关闭它,未打开就打开它
前面的博客记录了判断某个程序(比如计算器)是不是已经打开。这一篇博客进一步,如果程序打开了就关闭它,如果程序没有打开就打开它。
Sub killoropenprocess()
Dim strComputer As string
Dim objWMIService As Object
Dim colProcessList As Object
dim myshell as object
dim prog
Dim i As Object
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'calc.exe'")
If colProcessList.Count>0 Then
'关闭进程
For Each i In colProcessList
i.terminate
Next
set i=nothing
Else
set myshell =createobject("wscript.shell")
prog=myshell.run("c:\windows\system32\calc.exe",0)
myshell.quite
set myshell=nothing
End If
set colProcessList =nothing
set objWMIService =nothing
End Sub
红色部分原来忘记加了,实际使用过程中出现了问题。