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

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

问题描述

我很难找到如何使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和标题.然后,您可以将标题或URL与所需的标题或URL进行比较.下面的示例使用赞"功能比较标题,但是就像我说的,如果愿意,您可以比较网址.另外,如果您要查找的标题或网址没有变化,则可以使用"="代替"Like".

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

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

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