有没有更好的方法来完成此循环?

时间:2020-02-18 20:40:47

标签: vba loops readystate

我对VBA相当陌生,但是我一直在为我的团队制定程序。 这段代码大多数时候都可以工作,但有时会挂起。我不知道为什么有时会挂起,并且在大多数时候都能正常工作,所以我现在想找出一种更好的方法来完成此循环。我知道这种循环方法不是做事的最佳方法,但不确定如何完成任务。

我的网页在PEGA Web应用程序中运行,并且本机IE就绪状态指示器始终为“就绪”,因此我必须使用Web应用程序的就绪状态标记。

有人可以帮我吗?

Public Sub WaitingForRS()
' FIND THE C360 WINDOW
        Marker = 0
        Set objShell = CreateObject("Shell.Application")
        IE_count = objShell.Windows.Count
        For x = 0 To (IE_count - 1)
            On Error Resume Next
            my_url = objShell.Windows(x).Document.Location
            my_title = objShell.Windows(x).Document.title

            If my_title Like "Coverage User" & "*" Then
                Set C360Window = objShell.Windows(x)
                Marker = 1
                Exit For
            Else
            End If
        Next

        If Marker = 0 Then
            MsgBox ("C360 window is not found. Please ensure C360 is open in Internet Explorer and try again")
        Else
        End If

'FIND THE READY STATE INDICATOR
    RSIndicatorDocMarker = 0
    RSIndicatorDataMarker = 0
    Set RSIndicatorPage = C360Window.Document
    Set RSIndicatorClass = RSIndicatorPage.getelementsbyclassname("document-statetracker")(0)

RSIndicatorCheck:
'CHECK THE READY STATE DOC STATUS
    If RSIndicatorClass.getattribute("data-state-doc-status") = "ready" Then
        RSIndicatorDocMarker = 1
        Else: RSIndicatorDocMarker = 0
    End If

'CHECK THE READY STATE
    If RSIndicatorClass.getattribute("data-state-busy-status") = "none" Then
        RSIndicatorDataMarker = 1
        Else: RSIndicatorDataMarker = 0
    End If

'Compare the RSIndicators
    If RSIndicatorDataMarker = 1 And RSIndicatorDocMarker = 1 Then

    Else: GoTo RSIndicatorCheck
    End If
End Sub

2 个答案:

答案 0 :(得分:0)

在这篇帖子Excel VBA Submitting data via IE on an online MS Forms not working上查看我的答案

@想法5副标题。 WaitForLoadSETx()函数具有手动超越时间和双循环功能,以帮助捕获状态。

这是我可以在VBA中使用IE对象做的最好的事情。理想情况下,您想学习Selenium,PhantomJS或Puppeteer进行实际的浏览器操作。当然,这里有一些很酷的应用程序,人们已经在这些库的基础上提供了帮助(木偶录音机chrome可以添加进来)

或者将其保留在Excel中,请使用简单的XMLHTTPRequest对象将浏览器排除在等式之外,并仅处理来自网页的请求/响应。这是一个很好的选择,因为它使您可以集中精力以文本形式抓取html内容,而无需使用Javascript或等待页面加载。

答案 1 :(得分:0)

也许尝试使用OnTime代替当前的紧密循环:

Public Sub WaitingForRS()

    Dim win As Object
    Dim w As Object, el, ready As Boolean, idle As Boolean

    For Each w In CreateObject("Shell.Application").Windows
        If w.Name Like "*Internet*" Then
            If w.Title Like "Coverage user*" Then
                Set win = w
                Exit For
            End If
        End If
    Next

    If Not win Is Nothing Then

        Set el = win.document.getelementsbyclassname("document-statetracker")(0)
        ready = (el.getattribute("data-state-doc-status") = "ready")
        idle = (el.getattribute("data-state-busy-status") = "none")

        If ready And idle Then
            ProceedWithNextSteps win 'do whatever comes next: pass in the window
        Else
            'wait for a while then try again
            Application.OnTime Now + TimeSerial(0, 0, 1), "WaitingForRS"
        End If

    Else
        MsgBox "Window not found!"
    End If

End Sub

可能想添加一个时间限制,这样即使页面未“就绪”,它也不会永远循环播放。

相关问题