【问题标题】:Target existing IE window tab using VBA使用 VBA 定位现有的 IE 窗口选项卡
【发布时间】:2021-07-28 23:34:03
【问题描述】:

我正在尝试使用我的脚本。

换句话说:打开了一个带有特定选项卡的 Internet Explorer 窗口,我试图让我的脚本使用其选项卡名称来定位该选项卡。 我查看了这些主题,但没有一个对我有用(或者我做错了):

因此我创建了这个(工作了一段时间,直到我无缘无故地收到 438 错误):

Sub FindingExistingIE()

Dim Application As Object
Dim ApplicationWindows As Object
Dim WindowTitle As Variant
Dim TargetWindow As Object

Set Application = CreateObject("Shell.Application")
Set ApplicationWindows = Application.Windows

For Each Application In ApplicationWindows
    WindowTitle = Application.Document.Title
    If WindowTitle Like "IE Window Title" Then
        Set TargetWindow = Application
        Exit For
    End If
Next Application

NextSub TargetWindow

End
End Sub

正如我所说,脚本可以正常运行一周,但现在我收到 438 错误:

对象不支持此属性或方法(错误 438)

错误突出显示以下行:

WindowTitle = Application.Document.Title

阅读Microsoft Doc related to the error 后,我尝试将WindowTitle 变量更改为一个对象。没用。

有人有想法吗?

谢谢!

【问题讨论】:

  • 我在这里的回答对我来说一直很好 - stackoverflow.com/questions/31167200/… Shell Windows 集合还包括 Windows 资源管理器窗口,它没有 Document 属性,所以你必须处理这种情况.
  • 我用.Name 替换了.Document.Title,现在我有一个不同的错误! 91 错误...

标签: vba object internet-explorer


【解决方案1】:

这对我有用 - 通过使用传递的 URL 查找打开的窗口来查找 IE:

Sub tester()
    Dim w As Object
    
    Set w = FindingExistingIEByUrl("https://www.google.com")
    If Not w Is Nothing Then
        MsgBox w.document.Title
    Else
        MsgBox "No IE window found"
    End If
End Sub


Function FindingExistingIEByUrl(theUrl)
    Dim w As Object
    For Each w In CreateObject("Shell.Application").Windows
        'Debug.Print w.locationurl
        If w.locationurl Like theUrl & "*" Then
            Set FindingExistingIEByUrl= w
            Exit Function
        End If
    Next w
End Function


Function FindingExistingIEByTitle(theTitle)
    Dim w As Object, t As String
    For Each w In CreateObject("Shell.Application").Windows
        t = ""
        On Error Resume Next 'ignore error if no Document
        t = w.Document.Title
        On Error Goto 0
        If t Like theTitle Then
            Set FindingExistingIEByTitle = w
            Exit Function
        End If
    Next w
End Function

【讨论】:

  • 感谢蒂姆,但我尝试使用选项卡名称而不是 URL,因为每次运行脚本时后者都会更改
  • 见上面添加的FindingExistingIEByTitle - 也许试试看。
  • 蒂姆,你知道为什么没有错误处理程序脚本就不能工作吗?它可以正常工作,但没有错误 438。
  • 在你的问题下查看我的第一条评论
猜你喜欢
  • 1970-01-01
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
  • 2011-07-29
  • 1970-01-01
  • 2020-06-10
  • 2015-01-17
  • 1970-01-01
相关资源
最近更新 更多