如何在vba中获取网站页面的标题页?

时间:2019-06-25 17:10:13

标签: html vba internet-explorer

这是我一直在努力的一段代码:

Dim my_title2  as Variant
     Set objShell = CreateObject("Shell.Application")
        IE_count = objShell.Windows.Count
        MsgBox ("The number of pages is: " & IE_count)

        For x = 0 To (IE_count - 1)
        On Error Resume Next
        my_url = objShell.Windows(x).document.Location
         my_title = objShell.Windows(x).document.Title


            If my_title Like "F-Engine" & "*" Then
            Set ie = objShell.Windows(x)
             my_title2 = ie.document.Title
             'my_title2 = objShell.Windows(x).document.Title

            MsgBox ("The wanted title for the page should corrrespond. " & my_title2)
                   Exit For
        Else
        End If
    Next

设置Set ie = objShell.Windows(x)后,我无法打印窗口标题 当y_title2取值为ie.document.title时,MsgBox仅显示:

  

“页面所需的标题应该对应。”

此句后不打印任何内容。因此,不会显示分配给“ ie”的标题。

另一方面,如果my_title2取值为my_title2 = objShell.Windows(x).document.title,则实际上是在显示MsgBox

  

“页面所需的标题应该对应。”

后跟窗口名称。因此,显示的最终字符串将如下所示:

  

“该页面的所需标题应该对应。F-Engine”

我真的不明白为什么我无法使用第一个声明my_title2打印页面标题。

我正在做所有这些工作,以验证找到标题“ F-Engine”后是否正确地拾取了页面。为此,我正在尝试打印ie窗口标题的值。但是似乎没有任何设置和通过。

谁能帮助我弄清楚我的错误在哪里以及如何解决?

谢谢:)

1 个答案:

答案 0 :(得分:1)

并非objShell.Windows中的每个对象都代表一个IE页面/选项卡-它们可能是Windows资源管理器的实例。在这种情况下,没有文档属性可供访问。

您可以对此进行测试,而不必使用On Error Resume Next

Dim w As Object, myUrl, myTitle, ie

For Each w In CreateObject("Shell.Application").Windows
    If w.Name = "Internet Explorer" Then
        myUrl = w.document.Location
        myTitle = w.document.Title

        Debug.Print myUrl, myTitle

        If myTitle Like "F-Engine*" Then
            Set ie = w
            Debug.Print "Found: " & myTitle
            Exit For
        End If
    End If
Next w
相关问题