如何确保我的IE选项卡名称正确?

时间:2018-03-07 19:55:52

标签: vba ms-word word-vba screen-scraping

我正在使用此代码从单词VBA中获取Internet Explorer实例并从网页中抓取一些值。我正在循环浏览4个项目(以防万一,有时我不小心抓住了一个名为“Windows资源管理器”的东西,我不知道那是什么)来抓取Internet Explorer。但在我开始刮取值之前,我想确保我的标签名称是“概述 - 城市”。如何测试选项卡名称?

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

我尝试按照指南here使用此位在我找到它后尝试Debug.Print IE中的所有活动标签名称:

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

但是立即窗口返回空白而没有错误。我做错了什么?

2 个答案:

答案 0 :(得分:0)

标签只是另一个窗口。您可以使用下面的GetWebPage函数遍历窗口并获取您要查找的URL。

参考文献是 Microsoft Internet Controls Microsoft Shell控件和自动化

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

答案 1 :(得分:0)

以下是一些应该找到对打开的Internet Explorer选项卡的引用的代码。它通过循环遍历Shell.Application.Windows集合来完成此操作。该函数支持仅查找WindowName,URL和WindowName,并支持指定compare方法或者是否要进行匹配。我保持这段代码的后期绑定,以避免需要引用。

代码被评论,有些,如果有问题,请告诉我。

<强>代码

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