vba 使用VBA通过窗口标题获取IE窗口对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20664845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get IE window object by window title with VBA
提问by icfireball
I found this solution provided by @mkingston: How to intercept and manipulate a Internet Explorer popup with VBA
我找到了@mkingston 提供的这个解决方案: 如何使用 VBA 拦截和操作 Internet Explorer 弹出窗口
...but it's not working for me. I've added both of the reference libraries in question, but when I run the script I run into these issues:
......但它对我不起作用。我已经添加了两个有问题的参考库,但是当我运行脚本时,我遇到了这些问题:
Compile error: Undefined Sub due to pauseUntilIEReady (since this Sub wasn't included with the answer, I remove it from the script)
编译错误:Undefined Sub 由于 pauseUntilIEReady (由于此 Sub 未包含在答案中,因此我将其从脚本中删除)
Compile error: Argument not optional due to oGetIEWindowFromTitle (so I tried commeting this out to get the script to compile)
编译错误:由于 oGetIEWindowFromTitle,参数不是可选的(所以我尝试解决这个问题以编译脚本)
After the script finally compiles, it get this error:
脚本最终编译后,它收到此错误:
Automation error The system cannot find the file specified.
自动化错误 系统找不到指定的文件。
on this line of code: For Each oGetIEWindowFromTitle In objShellWindows
在这行代码上: For Each oGetIEWindowFromTitle In objShellWindows
Here's the code I'm trying to run:
这是我试图运行的代码:
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
and
和
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
回答by Tim Williams
This works for me.
这对我有用。
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