How can I make sure my IE tab name is correct?

2020-05-03 11:30发布

问题:

I'm using this code to grab the instance of Internet Explorer from word VBA and scraping some values from a webpage. I'm looping through 4 items (just in case, sometimes I've accidentally grabbed something called "Windows Explorer", which I have no idea what that is) to grab Internet Explorer. But before I begin scraping values, I want to make sure my tab name is "Overview - City". How can I test against the tab names?

Dim shellWins As ShellWindows, IE As InternetExplorer
Dim i As Long

Set shellWins = New ShellWindows

'Find Internet Explorer - if it can't find it, close the program
If shellWins.Count > 0 Then
    For i = 0 To 3
        On Error Resume Next
            If shellWins.Item(i).Name = "Internet Explorer" Then
                Set IE = shellWins.Item(i)
                Exit For
            End If
        On Error GoTo 0

        If i = 3 Then
            MsgBox "Could not find Internet Explorer.", vbExclamation, "Error"
            Exit Sub
        End If
    Next i
Else
    MsgBox "Could not find Internet Explorer.", vbExclamation, "Error"
    Exit Sub
End If

I tried following the guide here and used this bit to try and Debug.Print all the active tab names in IE once I had found it:

Dim IE_Tab As SHDocVw.InternetExplorer
Dim SH_Win As SHDocVw.ShellWindows

For each IE_Tab in SH_Win
    Debug.Print IE_Tab.Name 'This returns nothing?
Next IE_Tab

But the immediate window returns blank with no error. What am I doing wrong?

回答1:

Here is some code that should find a reference to the open Internet Explorer tab. It does this by looping through the Shell.Application.Windows collection. The function supports looking for just the WindowName, the URL and WindowName, and supports specifying the compare method or if you want to do a like match. I kept this code late bound, to avoid needing references.

The code is commented, somewhat, let me know if there are questions.

Code

Option Explicit

Private Function GetIEWindow(WindowName As String, _
                             ExactMatch As Boolean, _
                             Optional CompareMethod As VbCompareMethod = vbTextCompare, _
                             Optional URL As String) As Object

    Dim Window  As Object
    Dim Windows As Object: Set Windows = CreateObject("Shell.Application").Windows

    For Each Window In Windows
        'Make sure the app is Internet Explorer. Shell Windows can include other apps
        If InStr(1, Window.FullName, "IEXPLORE.EXE", vbTextCompare) > 0 Then
            'Perform exact matches, where the title or url and title match exactly
            If ExactMatch Then
                If Len(URL) = 0 Then
                    If Window.LocationName = WindowName Then
                        Set GetIEWindow = Window
                        Exit Function
                    End If
                Else
                    If Window.LocationName = WindowName And Window.LocationUrl = URL Then
                        Set GetIEWindow = Window
                        Exit Function
                    End If
                End If
            Else
            'Otherwise do a In String match
                If Len(URL) = 0 Then
                    If InStr(1, Window.LocationName, WindowName, CompareMethod) > 0 Then
                        Set GetIEWindow = Window
                        Exit Function
                    End If
                Else
                    If InStr(1, Window.LocationName, WindowName, CompareMethod) > 0 And InStr(1, Window.LocationUrl, URL, CompareMethod) > 0 Then
                        Set GetIEWindow = Window
                        Exit Function
                    End If
                End If
            End If
        End If

    Next

End Function

Sub ExampleUsage()
    Dim IE As Object: Set IE = GetIEWindow("exe", True)

    If Not IE Is Nothing Then
        Debug.Print "I found the IE window"
    Else
        Debug.Print "I didn't find the IE window"
    End If

End Sub


回答2:

The tab is just another window. You can use the GetWebPage function below to loop through the windows and get the URL you are looking for.

References are Microsoft Internet Controls Microsoft Shell Controls and Automation

Sub Example()
Dim ieWin As InternetExplorer

Set ieWin = CreateObject("InternetExplorer.Application")

    With ieWin
        .Navigate "https://www.google.com/"
        .Visible = True
        .Silent = True
    End With

    Set ieWin = GetWebPage("https://www.google.com/")

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Desc: The Function gets the Internet Explorer window that has the current
'   URL from the sURL Parameter.  The Function Timesout after 30 seconds
'Input parameters:
    'String sURL - The URL to look for
'Output parameters:
    'InternetExplorer ie - the Internet Explorer window holding the webpage
'Result: returns the the Internet Explorer window holding the webpage
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Function GetWebPage(sUrl As String) As InternetExplorer
Dim winShell As Shell
Dim dt As Date
dt = DateAdd("s", 300, DateTime.Now)

Dim ie As InternetExplorer

Do While dt > DateTime.Now
    Set winShell = New Shell
    'loop through the windows and check the internet explorer windows
    For Each ie In winShell.Windows
        If ie.LocationURL = sUrl Then
            Set GetWebPage = ie
            Do While ie.Busy
                DoEvents
            Loop
            Exit Do

        Set winShell = Nothing
        End If
    Next ie
    Set winShell = Nothing
    DoEvents
Loop
End Function