通过VBA - Excel终止所有的资源管理器实例 [英] Terminating all explorer instances via VBA - Excel
问题描述
设置objWMI = GetObject(winmgmts:/ $$$ /。)
设置objProcesses = objWMI.ExecQuery(Select * FROM Win32_Process Where Name ='iexplore.exe')
对于每个objProcess在objProcesses
objProcess.Terminate
下一个
设置objProcesses = Nothing
设置objWMI =没有
卸载WebForm
End Sub
尝试使用此方法来关闭所有的Explorer实例,然后再运行一些从基于Web的服务器检索数据的功能,但是当它尝试关闭具有多个选项卡的打开的浏览器时遇到问题。如果用户只有一个IE窗口(一个选项卡)打开,那么它关闭它只是罚款和移动;如果这些用户有多个窗口单独打开(不是在一个窗口中标签),那么它们都会很好地关闭它们并移动;但由于某些原因,如果一个窗口打开多个选项卡,那么我得到运行时错误'-2147217406(80041002)':未找到。解决方案
不完全是你所问的,而是另一种使用powerhell的方法,其中包括一个选项(来自此处关闭窗口,只有IE已经打开了超过X秒。
Sub Comesfast()
X2 = Shell(powershell.exe get-process iexplore |?{([DateTime] :: Now - $ _。StartTime).TotalSeconds -gt 05} | stop-process,1)
End Sub
Private Sub CommandButton1_Click()
Dim objWMI As Object, objProcess As Object, objProcesses As Object
Set objWMI = GetObject("winmgmts://.")
Set objProcesses = objWMI.ExecQuery("Select * FROM Win32_Process Where Name = 'iexplore.exe'")
For Each objProcess In objProcesses
objProcess.Terminate
Next
Set objProcesses = Nothing
Set objWMI = Nothing
Unload WebForm
End Sub
Trying to use this as a way to close out all instances of Explorer before running some functions that retrieve data from web based servers, but am running into an issue when it attempts to close out an open browser with multiple tabs. If the user has only one IE window (one tab) open, then it closes it just fine and moves on; if these user has multiple windows open separately (not tabbed in one window), then it closes them all just fine and moves on; but for some reason, if one window is open with multiple tabs, then I get run-time error '-2147217406 (80041002)': "Not found". Any help working through this would be greatly appreciated.
Not exactly what you asked, but another approach that uses powershell and includes an option (from here to close windows only where IE has been open for more than X seconds.
Sub Comesfast()
X2 = Shell("powershell.exe get-process iexplore | ? { ([DateTime]::Now - $_.StartTime).TotalSeconds -gt 05 } | stop-process", 1)
End Sub
这篇关于通过VBA - Excel终止所有的资源管理器实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!