简体   繁体   中英

VBA - terminating Internet Explorer processes via Shell

Including a line which terminates IE instances causes an automation error on Do Until or my macro freeze. The line is used to make sure no old processes sit in the memory.

The problem doesn't appear when I enter debugging, run the Shell line and wait a bit, so that probably IE can load before executing next lines.

I wonder is there is any way of detecting whether IE application is ready. Replacing name='iexplore.exe' with name='iexplore.exe *32' doesn't make the 32 bit processes disappear, maybe one disappears and to kill other instances a loop could be used?

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

WMIC just calls objects. You can call them direct.

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

It's name is IExplore.exe no matter which bitness.

To access shell folders (Explorer and Internet Explorer windows [historical reasons]). This refreshes all shell windows every 5 secs. I've added a msgbox in.

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM