通过部分URL获取活动的IE窗口

时间:2018-07-30 19:33:16

标签: vba excel-vba internet-explorer-11

我当前正在运行一些VBA来导航Intranet站点,执行一些操作等。我已经完成了需要执行的操作,现在我打开了2个IE窗口。我原来的一个窗口,我想保持打开状态。第二个窗口,我要关闭。

我在关闭第二个窗口时遇到问题。使用简单的SendKeys "%{F4}"根本不会关闭窗口,尽管如果我手动而不是通过VBA进行操作,则可以完全关闭窗口。另外,ieApp.Quit继续关闭第一个IE窗口,我想保持打开状态以再次使用。这很奇怪,因为当时它不是活动窗口。

我的问题是...是否有一种方法可以基于部分URL查找/选择打开的IE窗口?我知道网址将以http://ctdayppv02/PVEShowDocPopup.aspx?开头,但之后的所有内容都会每次更改。

我在网上看到了很多有关使用URL启动IE或返回已经打开的IE实例的URL的信息,但是我现在不打算做任何一个。我只想激活一个特定的打开的IE窗口,然后才可以关闭该窗口。

这是我的代码的一部分,到目前为止,该代码尚未关闭任何内容,也不会导致任何错误。这是最后一个无效的部分,其他一切都很好。:

'***** 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}"

关于如何仅关闭那一页的任何建议?预先感谢!

2 个答案:

答案 0 :(得分:1)

根据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

答案 1 :(得分:0)

我更喜欢Domenic的回复,但我想发布一种新的在线搜索方式,以帮助那些可能正在寻找这种方法并希望采用其他方法的人。

这种方式使用了在主子程序中调用的函数。 “查看文档”是显示在IE窗口标题中的文字,而不是URL中的文字。这将关闭在窗口标题中某处包含特定短语的任何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