简体   繁体   English

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

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

I am limited to using VBA to accomplish this.我仅限于使用 VBA 来完成此操作。 The goal is to programmatically Open a new IE window which is a duplicate of a window already open.目标是以编程方式打开一个新的 IE 窗口,该窗口是已打开窗口的副本。 I need to display this window for limited amount of time (in this example I am waiting 15 seconds), then I want to close one of the two IE windows I have open.我需要在有限的时间内显示这个窗口(在这个例子中我等待 15 秒),然后我想关闭我打开的两个 IE 窗口之一。 I have cobbled together code fragments from a few examples I have found and this is partially working, but the results are not as I would expect.我从我发现的几个例子中拼凑了代码片段,这部分工作,但结果并不像我预期的那样。

  • First I am able to find the IE instances but even though I think I have coded an exit, both windows are closed.首先,我能够找到 IE 实例,但即使我认为我已经对退出进行了编码,但两个窗口都已关闭。
  • The MsgBox I am using for debugging never appears.我用于调试的 MsgBox 从未出现。
  • With each run of the code the error message below appears每次运行代码时,都会出现以下错误消息

在此处输入图片说明

Below is the code I am trying to get to work, but failing with.下面是我试图开始工作但失败的代码。

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

I appreciate the input but had to go a slightly different route to get this working as it should...我很欣赏输入,但必须走一条稍微不同的路线才能使其正常工作......

The key to getting one instance of IE to close was solved by using TaskKill (commandline WScript).通过使用 TaskKill(命令行 WScript)解决了关闭 IE 实例的关键。

Below is the full solution下面是完整的解决方案

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