与已经打开的特定 IE 窗口交互的 VBA 代码 [英] VBA code to interact with specific IE window that is already open

查看:39
本文介绍了与已经打开的特定 IE 窗口交互的 VBA 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的过程:

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

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.

我正在尝试创建一个 VBA 脚本,该脚本将与我成功登录后打开的 IE 窗口进行交互(以开始自动执行一些更普通的任务).我在网上看过很多关于如何让 VBA 打开一个新的 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.

我已尝试将此作为快速测试,以查看是否可以选择正确的 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.

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

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

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.

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

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!

推荐答案

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

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".

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

这篇关于与已经打开的特定 IE 窗口交互的 VBA 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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