![](/img/trans.png)
[英]Close the download window in IE 9 once download completes using Excel vba
[英]VBA Excel - Open IE window to URL, pause, then close
我仅限于使用 VBA 来完成此操作。 目标是以编程方式打开一个新的 IE 窗口,该窗口是已打开窗口的副本。 我需要在有限的时间内显示这个窗口(在这个例子中我等待 15 秒),然后我想关闭我打开的两个 IE 窗口之一。 我从我发现的几个例子中拼凑了代码片段,这部分工作,但结果并不像我预期的那样。
下面是我试图开始工作但失败的代码。
Private Sub OpenReport()
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://~~~~~~~~~.net/reports/views/result/reportResult.faces"
' Wait for a period of time contained in TimeValue
Application.Wait (Now + TimeValue("00:00:15"))
' Now close ONE of the IE windows (Currently closing all of them)
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
' Find IE Instances
For Each objItem In colItems
If objItem.Name = "iexplore.exe" Then
On Error Resume Next
objItem.Terminate ' Terminates all instead of exiting after finding one IE window
MsgBox objItem.Name & " " & objItem.ProcessID & " " & objItem.CommandLine 'Doesn't appear
Exit For
End If
Next
End Sub
我很欣赏输入,但必须走一条稍微不同的路线才能使其正常工作......
通过使用 TaskKill(命令行 WScript)解决了关闭 IE 实例的关键。
下面是完整的解决方案
Private Sub OpenReport()
Dim IE
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://~~~~~~~~~~~net/reports/views/result/reportResult.faces"
' Wait for a period of time contained in TimeValue
Application.Wait (Now + TimeValue("00:00:15"))
' Now close ONE of the IE windows (Currently closing all of them)
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
' Find an IE instance
For Each objitem In colItems
If objitem.Name = "iexplore.exe" Then
On Error Resume Next
Shell ("TaskKill /PID " & objitem.ProcessID)
Exit For
End If
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.