用VBA窗口标题获取IE窗口对象 [英] Get IE window object by window title with VBA
问题描述
我发现通过@mkingston提供此解决方案:
<一href=\"http://stackoverflow.com/questions/14446951/solved-how-to-intercept-and-manipulate-a-internet-explorer-popup-with-vba\">(Solved)如何拦截和操纵使用VBA 一个Internet Explorer的弹出
...但它不是为我工作。我添加这两个问题的参考库,但是当我运行该脚本,我碰到了这些问题:
编译错误:未定义子因pauseUntilIEReady(因为这个潜水艇不附带答案,我从剧本中删除)
编译错误:参数不可选因oGetIEWindowFromTitle(所以我尝试commeting这一点让脚本编译)
脚本编译终于之后,得到这个错误:
自动化错误
该系统找不到指定的文件。
在这条线code的:
对于每个oGetIEWindowFromTitle在objShellWindows
这里的code我试图运行:
功能oGetIEWindowFromTitle(sTitle作为字符串_
可选bCaseSensitive由于布尔=假,_
可选bExact由于布尔= FALSE)作为SHDocVw.InternetExplorer昏暗objShellWindows作为新SHDocVw.ShellWindows
昏暗的发现作为布尔
昏暗的startTime单找到=假
通过shell窗口'循环
对于每个oGetIEWindowFromTitle在objShellWindows
找到= oGetIEWindowFromTitleHandler(oGetIEWindowFromTitle,sTitle,bCaseSensitive,bExact)
如果找到则退出对于
下一个检查一个窗口是否被发现
如果找不到,则
设置oGetIEWindowFromTitle =什么
其他
注释,以GET脚本来编译pauseUntilIEReady oGetIEWindowFromTitle
万一结束功能
专用功能oGetIEWindowFromTitleHandler(赢SHDocVw.InternetExplorer,_
sTitle作为字符串_
bCaseSensitive由于布尔,_
bExact由于布尔)为布尔oGetIEWindowFromTitleHandler = FALSE对错误转到处理程序
如果文件是类型HTMLDocument的,它是一个IE窗口
如果类型名(win.Document)=HTMLDocument的然后
'检查标题是否包含标题传递
如果bExact然后
如果(win.Document.title = sTitle)或((不bCaseSensitive)和(LCASE(sTitle)= LCASE(win.Document.title)))然后oGetIEWindowFromTitleHandler = TRUE
其他
如果INSTR(1,win.Document.title,sTitle)或((不bCaseSensitive)和(INSTR(1,LCASE(win.Document.title),LCASE(sTitle),vbTextCompare)所述;&0))然后oGetIEWindowFromTitleHandler = TRUE
万一
万一
处理程序:
我们在这里,如果引发一个错误,那是因为假设
窗外是类型不正确。因此,我们
简单地忽略它而矣。结束功能
和
子测试()昏暗的IE作为SHDocVw.InternetExplorer
昏暗的DOC作为HTMLDocument的如果你有到HTML对象库的引用
昏暗的DOC作为对象'如果你没有到HTML对象库的引用根据需要在此处更改标题
设置IE = oGetIEWindowFromTitle(我的弹出窗口)
设置文档= ie.DocumentDebug.Print doc.getElementsByTagName(身体)项目(0).innerText结束小组
这为我工作。
功能IEWindowFromTitle(sTitle作为字符串)作为SHDocVw.InternetExplorer 昏暗objShellWindows作为新SHDocVw.ShellWindows
昏暗的胜利为对象,RV作为SHDocVw.InternetExplorer 对于每赢在objShellWindows
如果类型名(win.Document)=HTMLDocument的然后
如果用Ucase(win.Document.Title)=用Ucase(sTitle)然后
设置RV =胜利
对于出口
万一
万一
下一个 设置IEWindowFromTitle = RV结束功能子仪() 昏暗W¯¯作为SHDocVw.InternetExplorer
集合W = IEWindowFromTitle(谷歌)
如果不是W是没有那么
Debug.Print w.Document.Title
其他
Debug.Print未找到
万一结束小组
I found this solution provided by @mkingston: (Solved) How to intercept and manipulate a Internet Explorer popup with VBA
...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)
Compile error: Argument not optional due to oGetIEWindowFromTitle (so I tried commeting this out to get the script to compile)
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
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
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
这篇关于用VBA窗口标题获取IE窗口对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!