我发现@mkingston提供此解决方案: How to intercept and manipulate a Internet Explorer popup with VBA获取IE窗口对象的窗口标题用VBA
...但它不是为我工作。我已经添加了两个有问题的参考库,但是当我运行脚本时遇到以下问题:
编译错误:未定义由于pauseUntilIEReady子(因为此Sub未包括在答案中,我删除它从脚本)
编译错误:参数不可选由于oGetIEWindowFromTitle(所以我尝试commeting这一点得到脚本编译)
剧本终于编译后,出现此错误:
自动化错误 系统找不到指定的文件。
在这行代码: 对于每个oGetIEWindowFromTitle在objShellWindows
这里是我试图运行代码:
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
发布您正在尝试使用的实际代码。 –
您好,我最初没有发布代码,因为它在链接解决方案中的建议是一样的。我在下面复制它,其中一行注释了我必须评论才能获得编译代码。 – icfireball