如何确保我的 IE 选项卡名称正确? [英] How can I make sure my IE tab name is correct?

查看:63
本文介绍了如何确保我的 IE 选项卡名称正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码从 Word VBA 中获取 Internet Explorer 实例并从网页中抓取一些值.我正在遍历 4 个项目(以防万一,有时我不小心抓住了一个叫做Windows 资源管理器"的东西,我不知道那是什么)来抓住 Internet Explorer.但在开始抓取值之前,我想确保我的选项卡名称是概览 - 城市".如何针对选项卡名称进行测试?

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

我尝试按照指南here 并使用此位尝试 Debug.Print 找到 IE 中的所有活动选项卡名称:

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?

推荐答案

下面是一些代码,可以找到对打开的 Internet Explorer 选项卡的引用.它通过循环遍历 Shell.Application.Windows 集合来做到这一点.该函数支持仅查找 WindowName、URL 和 WindowName,并支持指定比较方法或是否要进行类似匹配.我保留这段代码后期绑定,以避免需要引用.

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.

代码

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

这篇关于如何确保我的 IE 选项卡名称正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆