【问题标题】:Get IE window object by window title with VBA使用 VBA 通过窗口标题获取 IE 窗口对象
【发布时间】:2014-01-07 00:38:37
【问题描述】:

我找到了@mkingston 提供的这个解决方案: How to intercept and manipulate a Internet Explorer popup with VBA

...但这对我不起作用。我已经添加了两个有问题的参考库,但是当我运行脚本时,我遇到了这些问题:

编译错误:由于 pauseUntilIEReady 导致的未定义 Sub(由于此 Sub 未包含在答案中,因此我将其从脚本中删除)

编译错误:参数不是可选的,因为 oGetIEWindowFromTitle (所以我尝试解决这个问题以编译脚本)

脚本最终编译后,报错:

自动化错误 系统找不到指定的文件。

在这行代码上: 对于 objShellWindows 中的每个 oGetIEWindowFromTitle

这是我要运行的代码:

Function oGetIEWindowFromTitle(sTitle As String, _
                           Optional bCaseSensitive As Boolean = False, _
                           Optional bExact As Boolean = False) As SHDocVw.InternetExplorer

Dim objShellWindows As New SHDocVw.ShellWindows
Dim found As Boolean
Dim startTime As Single

found = False
'Loop through shell windows
For Each oGetIEWindowFromTitle In objShellWindows
    found = oGetIEWindowFromTitleHandler(oGetIEWindowFromTitle, sTitle, bCaseSensitive, bExact)
    If found Then Exit For
Next

'Check whether a window was found
If Not found Then
    Set oGetIEWindowFromTitle = Nothing
Else
    'COMMENTED OUT TO GET SCRIPT TO COMPILE pauseUntilIEReady oGetIEWindowFromTitle
End If

End Function


Private Function oGetIEWindowFromTitleHandler(win As SHDocVw.InternetExplorer, _
                                  sTitle As String, _
                                  bCaseSensitive As Boolean, _
                                  bExact As Boolean) As Boolean

oGetIEWindowFromTitleHandler = False

On Error GoTo handler
'If the document is of type HTMLDocument, it is an IE window
If TypeName(win.Document) = "HTMLDocument" Then
    'Check whether the title contains the passed title
    If bExact Then
        If (win.Document.title = sTitle) Or ((Not bCaseSensitive) And (LCase(sTitle) = LCase(win.Document.title))) Then oGetIEWindowFromTitleHandler = True
    Else
        If InStr(1, win.Document.title, sTitle) Or ((Not bCaseSensitive) And (InStr(1, LCase(win.Document.title), LCase(sTitle), vbTextCompare) <> 0)) Then oGetIEWindowFromTitleHandler = True
    End If
End If
handler:
'We assume here that if an error is raised it's because
'the window is not of the correct type. Therefore we
'simply ignore it and carry on.

End Function

和

Sub test()

Dim ie As SHDocVw.InternetExplorer
Dim doc As HTMLDocument 'If you have a reference to the HTML Object Library
'Dim doc as Object 'If you do not have a reference to the HTML Object Library

' Change the title here as required
Set ie = oGetIEWindowFromTitle("My popup window")
Set doc = ie.Document

Debug.Print doc.getElementsByTagName("body").Item(0).innerText

End Sub

【问题讨论】:

  • 发布您尝试使用的实际代码。
  • 嗨蒂姆,我最初没有发布代码,因为它与链接解决方案中提出的相同。我在下面复制了它,其中一行注释掉了我必须注释才能编译代码。

标签: internet-explorer vba automation


【解决方案1】:

这对我有用。

Function IEWindowFromTitle(sTitle As String) As SHDocVw.InternetExplorer

    Dim objShellWindows As New SHDocVw.ShellWindows
    Dim win As Object, rv As SHDocVw.InternetExplorer

    For Each win In objShellWindows
        If TypeName(win.Document) = "HTMLDocument" Then
            If UCase(win.Document.Title) = UCase(sTitle) Then
                Set rv = win
                Exit For
            End If
        End If
    Next

    Set IEWindowFromTitle = rv

End Function

Sub Tester()

    Dim w As SHDocVw.InternetExplorer
    Set w = IEWindowFromTitle("Google")
    If Not w Is Nothing Then
        Debug.Print w.Document.Title
    Else
        Debug.Print "Not found"
    End If

End Sub

【讨论】:

  • 感谢蒂姆的回复。当我运行它时,我得到:“自动化错误/系统找不到指定的文件”,VBA 在这一行中断:“For Each win In objShellWindows”
  • 对我来说很好。 Win7 64 位上的 32 位 Excel。
  • 尝试在两台运行 Win7 的不同计算机上安装 Excel 2010。除了 Microsoft Internet Controls 和 Microsoft HTML Object Library,我还需要哪些参考库?
  • 好的,我修改了脚本以包含For Each win In objShellWindows / MsgBox (win.Name) / Next,它返回“Windows Internet Explorer”(并且使用 LocationURL 返回 url)但是 win.Document 或 win.Document.Name 失败,因为它说 Document is '不是一个有效的属性或方法。
  • 您需要参考Microsoft Shell Controls and Automation
猜你喜欢
  • 2013-05-09
  • 2023-04-08
  • 1970-01-01
  • 2010-12-05
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 2016-01-29
  • 2012-04-16
相关资源
最近更新 更多