VBA Excel - 打开 IE window 到 URL,暂停,然后关闭

VBA Excel - Open IE window to URL, pause, then close

我仅限于使用 VBA 来完成此操作。目标是以编程方式打开一个新的 IE window,它是已打开的 window 的副本。 我需要在有限的时间内显示此 window(在此示例中我等待 15 秒),然后我想关闭我打开的两个 IE windows 之一。 我从我发现的几个示例中拼凑了代码片段,这部分有效,但结果并不像我预期的那样。

下面是我试图开始工作但失败的代码。

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

我很感谢您的意见,但必须采取稍微不同的方式才能使其正常工作...

关闭一个 IE 实例的关键已通过使用 TaskKill(命令行 WScript)解决。

下面是完整的解决方案

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