Excel VBA:停止和启动后台服务

时间:2019-03-01 10:44:27

标签: excel vba service

是否可以停止和启动后台服务。

有第三方服务干扰Excel插件。我想在运行代码时暂时将其停止,然后最后将其重新打开。

1 个答案:

答案 0 :(得分:0)

只要您想使用Google的“ WMI”功能进行操作,

例如-

发件人:https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/stopservice-method-in-class-win32-service

Set ServiceSet = GetObject("winmgmts:").ExecQuery( _
          "select * from Win32_Service where Name='ClipSrv'")

for each Service in ServiceSet
 RetVal = Service.StopService()
 if RetVal = 0 then 
  WScript.Echo "Service stopped" 
 elseif RetVal = 5 then 
  WScript.Echo "Service already stopped" 
 end if
next

类似地:https://docs.microsoft.com/en-us/windows/desktop/cimwin32prov/startservice-method-in-class-win32-service

Set ServiceSet = GetObject("winmgmts:").ExecQuery( _
           "select * from Win32_Service where Name='ClipSrv'")

for each Service in ServiceSet
 RetVal = Service.StartService()
 if RetVal = 0 then WScript.Echo "Service started"
 if RetVal = 10 then WScript.Echo "Service already running"
next