簡體   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