使用For Each窗口shell应用程序捕获IE窗口

时间:2017-03-28 07:51:44

标签: vba excel-vba excel

我需要捕获窗口并需要进一步导航,我能够使用以下代码进行捕获,

With objIE
    Application.Wait (Now + TimeValue("00:00:05"))
    Set TBL = .document.getElementById("payersitecredential-table")
    Set PRV = TBL.getElementsByTagName("A"): PRV.Item(1).Click
    'once the above code runs, it opens up an another window and i need to set that window as object.
    Application.Wait (Now + TimeValue("00:00:20"))
End With
For Each WINDW In CreateObject("Shell.Application").Windows
    If WINDW.Name = "Internet Explorer" Then
        If InStr(WINDW.LocationURL, "newmmis") <> 0 Then
            Set objIE1 = WINDW
            Exit For
        ElseIf InStr(WINDW.LocationURL, "https://www.example.com") <> 0 Then
            Set ClsWindw = WINDW
            ClsWindw.Quit
            GoTo ThEnD
        End If
    End If
Next
With objIE1
    Do While .Busy Or .readyState <> 4 'this is the place i get error as object required
        DoEvents
    Loop
    Application.Wait (Now + TimeValue("00:00:05"))
    .document.getElementById("InquireStatus").Click
End With

我保持加载URL的等待时间为60秒,但有时URL需要很长时间才能加载,那时它无法设置对象,你们可以让我弄清楚这个并给我解决方案。

1 个答案:

答案 0 :(得分:0)

请参阅此内容以支持我的评论

Sub ex()

Dim ie As SHDocVw.InternetExplorer
Dim wins As New SHDocVw.ShellWindows

For Each ie In wins
    Debug.Print ie.Document.URL, ie.ReadyState, ie.Busy
Next ie

End Sub

修改

我会利用上面的内容,就像这样,这会找到基于URL的IE并等到它准备就绪并返回,所以请使用set IE=GetMeReadyIE("http://stackoverflow.com/questions/tagged/vba")

Function GetMeReadyIE(strURL As String) As SHDocVw.InternetExplorer

Dim ie As SHDocVw.InternetExplorer
Dim wins As New SHDocVw.ShellWindows

For Each ie In wins
    If ie.Document.URL = strURL Then
        While ie.ReadyState <> READYSTATE_COMPLETE Or ie.Busy
            DoEvents
        Wend
        Set GetMeReadyIE = ie
        Exit For
    End If
Next ie

End Function