首页 > 解决方案 > 使用 VBA 导航具有相同窗口标题的第二个窗口

问题描述

我经常浏览一个 Intranet 网站,该网站在单击第一个链接后会打开第二个窗口。我想更改第二个窗口中“textarea”元素的值,但在关注正确的窗口时遇到问题。关于我应该如何继续以下代码以实现我想要做的事情有什么建议吗?

IE.Navigate webPageURL

PageLoadWait 'sub that checks IE.busy and IE.readystate

With IE.Document.Frames("topFrame").document

   .getElementById("user")=userID

   .getElementById("pwd")=password

   .getElementById("submit"). Click

End With

PageLoadWait 'Second window opened

我查看了下面的问题,但不确定如何使其适应我的需要,因为两个窗口的窗口标题相同。我尝试匹配窗口 URL,因为它们略有不同,但由于某种原因无法成功。

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

标签: excelvba

解决方案


假设您知道第二个窗口 URL,请尝试下一个功能:

Private Function SecIE(strURL As String) As InternetExplorer
    ' It needs a reference to 'Microsoft Internet Controls'
    Dim objIE As Object
    For Each objIE In CreateObject("Shell.Application").Windows
        If objIE.Name = "Internet Explorer" Then
            If objIE.Document.Location = strURL Then
                Set SecIE = objIE: Exit Function
            End If
        End If
    Next
End Function

可以这样调用:

Sub testFindIEObject()
   Dim secondIE As InternetExplorer
   Set secondIE = SecIE("http://www.msn.com/en-xl/?ocid=iehp&AR=1") 'use your sec Wind URL
   Debug.Print secondIE.Document.Title
End Sub

我只是出于测试原因使用他上面的 URL...

如果您不知道,无法提取第二个窗口 URL,我想我可以调整代码以找到第二个,从它的标题开始。必须创建一个静态变量并计算具有相同标题的窗口。第一个窗口将被跳过,第二个窗口将被定义为希望的 IE 对象。


推荐阅读