与已经打开的特定 IE 窗口交互的 VBA 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21837844/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:48:21  来源:igfitidea点击:

VBA code to interact with specific IE window that is already open

internet-explorervbaexcel-vbaexcel

提问by user1238528

I am having trouble finding out how to get excel VBA to interact with a specific IE Window that is already open.

我无法找到如何让 excel VBA 与已经打开的特定 IE 窗口交互。

Here my process:

这是我的过程:

I open IE, navigate to the website, enter my username and pw, click log in. A new IE window opens that I use to enter member numbers and search for membership info.

我打开 IE,导航到网站,输入我的用户名和密码,点击登录。一个新的 IE 窗口打开,我用来输入会员号和搜索会员信息。

I am trying to create a VBA script that will interact with the IE window that opens after I successfully log in (to begin to automate some of the more mundane tasks). I have seen tons of tutorials online about how to have VBA open a new IE window, and then interact with it. However, I am really struggling to find info about how to select a specific already open IE window and interact with it.

我正在尝试创建一个 VBA 脚本,该脚本将与成功登录后打开的 IE 窗口进行交互(以开始自动执行一些更普通的任务)。我在网上看过很多关于如何让 VBA 打开一个新的 IE 窗口,然后与之交互的教程。但是,我真的很难找到有关如何选择特定的已经打开的 IE 窗口并与之交互的信息。

I have tried this as a quick test to see if I could select the correct IE window as an object that I can interact with.

我已尝试将此作为快速测试,以查看是否可以选择正确的 IE 窗口作为可以与之交互的对象。

Sub getIE()
Dim sh As Object, oWin As Object, IE As Object

Set sh = CreateObject("Shell.Application")

For Each oWin In sh.Windows
    If TypeName(oWin.document) = "HTMLDocument" Then
        Set IE = oWin
        Exit For
    End If
Next

MsgBox IE.document.URL

End Sub

The message box appears and shows the URL for initial IE window, but I don't know how to get it to select the IE window that I typically use to actually get member info.

消息框出现并显示初始 IE 窗口的 URL,但我不知道如何让它选择我通常用于实际获取成员信息的 IE 窗口。

I took several technical classes years ago when I was in school, but has been a while since I have flexed any tech muscles. Any help would be greatly appreciated!

几年前我在学校时参加了几门技术课程,但我已经有一段时间没有展示任何技术肌肉了。任何帮助将不胜感激!

回答by ron

Here's what I use. It counts the number of open instances of IE and then steps through each instance and determines the url and title of the web page. You can then compare the title or url against the title or url you are looking for. The following example compares titles using the "Like" function, but like I said you could compare urls if you like. Also, if you are looking for a title or url that doesn't change you can use "=" instead of "Like".

这是我使用的。它计算 IE 的打开实例数,然后逐步遍历每个实例并确定网页的 url 和标题。然后,您可以将标题或网址与您要查找的标题或网址进行比较。下面的示例使用“Like”函数比较标题,但就像我说的,如果您愿意,可以比较 url。此外,如果您正在寻找不变的标题或网址,您可以使用“=”代替“Like”。

marker = 0
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
    On Error Resume Next    ' sometimes more web pages are counted than are open
    my_url = objShell.Windows(x).Document.Location
    my_title = objShell.Windows(x).Document.Title

    If my_title Like "XYZ" & "*" Then 'compare to find if the desired web page is already open
        Set ie = objShell.Windows(x)
        marker = 1
        Exit For
    Else
    End If
Next

If marker = 0 then
    msgbox("A matching webpage was NOT found")
Else
    msgbox("A matching webpage was found")
End If

回答by Steven Alpha

The above code worked well for me. I turned it into a function call as follows

上面的代码对我来说效果很好。我把它变成了一个函数调用如下

Set ieFindUser = FindIEObject("Find and List Users")            'Find handle for popup window.

The Function

功能

Public Function FindIEObject(target As String) As InternetExplorerMedium
    Set objShell = CreateObject("Shell.Application")
    IE_count = objShell.Windows.Count
    For x = 0 To (IE_count - 1)
        On Error Resume Next    ' sometimes more web pages are counted than are open
        my_url = objShell.Windows(x).Document.Location
        my_title = objShell.Windows(x).Document.Title
        If my_title = target Then 'compare to find if the desired web page is already open
            Set FindIEObject = objShell.Windows(x)
            Exit For
        End If
    Next
End Function