2016-10-02 112 views
0

包含终止IE实例的行导致Do Until或我的宏冻结时出现自动化错误。该行用于确保没有旧的进程坐在内存中。VBA - 通过Shell终止Internet Explorer进程

当我进入调试,运行Shell行并等待一点时,这个问题没有出现,所以IE可以在执行下一行之前加载。

我不知道是否有任何方式来检测IE应用程序是否准备就绪。用name='iexplore.exe *32'替换name='iexplore.exe'不会使32位进程消失,也许一个消失并杀死其他实例可以使用循环?

Shell ("C:\Windows\system32\cmd.exe /c wmic process where name='iexplore.exe' call terminate") 

Link = "http://www.example.com" 
Set ie = CreateObject("InternetExplorer.Application") 

ie.Navigate Link 

Do Until ie.ReadyState = 4 And ie.Busy = False 
    DoEvents 
Loop 

回答

1

WMIC只是调用对象。你可以直接打电话给他们。

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") 

Set colItems = objWMIService.ExecQuery("Select * From Win32_Process") 

For Each objItem in colItems 
    'msgbox objItem.name & " " & objItem.ProcessID & " " & objItem.CommandLine 
    If objItem.name = "iexplore.exe" then objItem.terminate 
Next 

它的名字是IExplore.exe,不管是哪个位。

访问shell文件夹(资源管理器和Internet Explorer窗口[历史原因])。这会每5秒刷新一次所有的外壳窗口。我已添加msgbox。

Set objShell = CreateObject("Shell.Application") 
    Do 
     Set AllWindows = objShell.Windows 
     Count = 0 
     For Each window in AllWindows 
      msgbox window.locationname 
      window.refresh2 3 
`To close a shell window 
`window.close 
      Count = Count + 1 
     Next 
     If Count = 0 then Exit Do 
     Wscript.sleep 5000 
    Loop