如何拦截和操作与VBA中的Internet Explorer弹出 [英] How to intercept and manipulate a Internet Explorer popup with VBA

查看:597
本文介绍了如何拦截和操作与VBA中的Internet Explorer弹出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

语言/软件:

语言是VBA。该应用程序是Access 2003中(我也可以使用Excel)和Internet Explorer(在Windows XP /七)。

The language is VBA. The application is Access 2003 (I also can use Excel) and Internet Explorer (on Windows XP/Seven).

问题:

我正在开发它打开并操作,我的工作对企业的内部网站访问一个宏。

I'm developing a Access macro which opens and manipulates a intranet site of the enterprise where I work.

我可以创建新的IE窗口,并在填写表格的数据,但我需要能够拦截和操纵其他的IE窗口,如弹出窗口,当我点击一个链接,这将打开,当我选择的选项选择或当页面加载元素。

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.

推荐答案

下面是一些code我用得到一个IE窗口,从它的标题。它分为两个功能,因为我的用户之一是有一个非常奇怪的问题,即没有被正确捕获的错误。你可能会(可能)能够在私有函数体移动到公共职能。

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.

此外,你需要设置为Microsoft Internet控制的引用(这是不是shdocvw.dll中或ieframe.dll,根据您的Windows版本),我建议你设置一个参考Microsoft HTML对象库,以使其更容易遍历DOM,一旦你有你的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.

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

如下使用上述code:

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 :).

这篇关于如何拦截和操作与VBA中的Internet Explorer弹出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆