繁体   English   中英

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

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

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

  • 首先,我能够找到 IE 实例,但即使我认为我已经对退出进行了编码,但两个窗口都已关闭。
  • 我用于调试的 MsgBox 从未出现。
  • 每次运行代码时,都会出现以下错误消息

在此处输入图片说明

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

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.

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