如何使用 VBA 拦截和操作 Internet Explorer 弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14446951/
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
How to intercept and manipulate a Internet Explorer popup with VBA
提问by Nathan Nathan
Language/Software:
语言/软件:
The language is VBA. The application is Access 2003 (I also can use Excel) and Internet Explorer (on Windows XP/Seven).
语言是VBA。该应用程序是 Access 2003(我也可以使用 Excel)和 Internet Explorer(在 Windows XP/Seven 上)。
The problem:
问题:
I'm developing a Access macro which opens and manipulates a intranet site of the enterprise where I work.
我正在开发一个 Access 宏,它可以打开和操作我工作所在企业的 Intranet 站点。
I can create new IE windows and fill data in the forms, but I need to be able of intercept and manipulate other IE windows, such as popups, which opens when I click on a link, when I choose an option of a select element or when the page is loaded.
我可以创建新的 IE 窗口并在表单中填充数据,但我需要能够拦截和操作其他 IE 窗口,例如弹出窗口,当我单击链接、选择选择元素的选项或页面加载时。
采纳答案by mkingston
Here's some code I use to get an IE window from it's title. It's split into two functions because one of my users was having an extremely odd problem where an error was not being caught properly. You might (probably will) be able to move the body of the private function into the public function.
这是我用来从标题中获取 IE 窗口的一些代码。它分为两个功能,因为我的一个用户遇到了一个非常奇怪的问题,错误没有被正确捕获。您可能(可能会)能够将私有函数的主体移动到公共函数中。
Additionally, you'll need to set a reference to Microsoft Internet Controls (this is either shdocvw.dll or ieframe.dll, depending on your version of Windows) and I'd recommend setting a reference to Microsoft HTML Object Library to make it easier to traverse the DOM once you have your IE object.
此外,您需要设置对 Microsoft Internet Controls 的引用(这是 shdocvw.dll 或 ieframe.dll,具体取决于您的 Windows 版本),我建议设置对 Microsoft HTML 对象库的引用以使其更容易一旦你有了你的 IE 对象,就可以遍历 DOM。
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
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
Use the above code as follows:
使用上面的代码如下:
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
You can find a window from almost any property of the window, or it's document contents. If you're struggling with this, please comment :).
您几乎可以从窗口的任何属性或文档内容中找到该窗口。如果您为此而苦苦挣扎,请发表评论:)。