通过部分 URL 获取活动 IE window

Get an active IE window by partial URL

我目前 运行 一些 VBA 浏览内部网站、执行一些操作等。我已经完成了我需要采取的操作,现在我有 2 个 IE windows 打开。一个window,我的原创,我想保持开放。第二个window,我要关闭

我在关闭第二个 window 时遇到问题。使用简单的 SendKeys "%{F4}" 根本不会关闭 window,尽管如果我手动而不是通过 VBA 执行这些步骤它会关闭。此外,ieApp.Quit 一直关闭第一个 IE window,我想保持打开状态以再次使用。这很奇怪,因为它当时不是活动的 window。

我的问题是...有没有办法 find/select 基于部分 URL 的开放式 IE window?我知道 URL 将以 http://ctdayppv02/PVEShowDocPopup.aspx? 开头,但之后的内容每次都会更改。

我在网上看到很多有关使用 URL 启动 IE 或返回已打开的 IE 实例的 URL 的内容,但我现在不想做其中任何一个观点。我只想激活一个特定的打开的 IE window,这样我就可以只关闭那个。

这是我的部分代码,目前没有关闭任何内容,但也不会导致任何错误。就是最后那个不行,其他都还好。:

'***** Click the Search button *****
        ieApp.Document.all.Item("btnSubmitProjectSearch").Click: DoEvents: Sleep 1000

'***** Click the Print button *****
        ieApp.Document.all.Item("printLink").Click: DoEvents: Sleep 1000

'***** Setting variables for Windows API. Will be used to find the proper windows box by name *****
        Dim windowHWND As LongPtr
        Dim buttonHWND As LongPtr
        Dim hwnd As String
        Dim hwindow2 As String

''***** Will click the Print button. MUST HAVE MICROSOFT PDF AS DEFAULT PRINTER *****
        windowHWND = getWindowPointer("Print", "#32770")

        If windowHWND > 0 Then
        '***** Add a small delay to allow the window to finish rendering if needed *****
            Sleep 250
            buttonHWND = FindWindowEx(windowHWND, 0, "Button", "&Print")
            If buttonHWND > 0 Then
            SendMessage buttonHWND, BM_CLICK, 0, 0
            Else
                Debug.Print "didn't find button!"
            End If
        End If

'***** Locate the "Save Print Output As" window, enter the filepath/filename and press ENTER *****
        hwnd = FindWindow(vbNullString, "Save Print Output As")
        Do
        DoEvents
        hwindow2 = FindWindow(vbNullString, "Save Print Output As")

        Loop Until hwindow2 > 0

        SendKeys "C:\Users\NAME\Documents\" & Range("G2").Value
        SendKeys "{ENTER}"

'***** Locate the Viewer Control box that appears after saving and press ENTER to advance *****
        hwnd = FindWindow(vbNullString, "PaperVision Document Viewer Control")
        Do
        DoEvents
        hwindow2 = FindWindow(vbNullString, "PaperVision Document Viewer Control")

        Loop Until hwindow2 > 0

        SendKeys "{Enter}"

'***** Locate the "PaperVision - View Document" IE window and close it *****
        hwnd = FindWindow(vbNullString, "PaperVision - View Document - Internet Explorer")
        Do
        DoEvents
        hwindow2 = FindWindow(vbNullString, "PaperVision - View Document - Internet Explorer")

        Loop Until hwindow2 > 0

        'ieApp.Quit
        SendKeys "%{F4}"

关于如何只关闭那一页有什么建议吗?提前致谢!

根据 QHarr 的第一个建议,尝试...

Option Explicit

Sub test()

    Dim oShell As Object
    Dim oShellWindows As Object
    Dim oShellWindow As Object
    Dim sPartialURL As String

    sPartialURL = "http://ctdayppv02/PVEShowDocPopup.aspx?"

    Set oShell = CreateObject("Shell.Application")
    Set oShellWindows = oShell.Windows

    For Each oShellWindow In oShellWindows
        If oShellWindow.Name = "Internet Explorer" Then
            If InStr(oShellWindow.Document.URL, sPartialURL) > 0 Then
                Exit For
            End If
        End If
    Next oShellWindow

    If Not oShellWindow Is Nothing Then
        'Do stuff
        '
        '
        oShellWindow.Quit
    Else
        MsgBox "The specified Internet Explorer window was not found!", vbExclamation
    End If

    Set oShell = Nothing
    Set oShellWindows = Nothing
    Set oShellWindow = Nothing

End Sub

我更喜欢 Domenic 的回应,但我想 post 我在网上遇到的另一种方式,供任何可能在路上看到这个并想要另一种方法的人使用。

这种方式使用了在主子中调用的函数。 "View Document" 是出现在 IE window 标题中的措辞,而不是 URL 中的措辞。这将关闭任何在 window 标题中某处包含该特定短语的 IE。我只测试了几次,但它似乎有效。

Sub CloseWindow()
    Do Until Not CloseIeIf("View Document"): Loop
End Sub

Function CloseIeIf(Str As String) As Boolean
    Dim ie As Object

    For Each ie In CreateObject("Shell.Application").Windows
        If InStr(ie.LocationName, Str) <> 0 Then
            ie.Quit
            CloseIeIf = True
        End If
    Next
End Function