Vba 有没有更好的方法来完成这个循环?
我对VBA很陌生,但我一直在为我的团队制作一个程序。 这段代码大部分时间都有效,但有时会挂起。我不明白为什么它有时会挂起,并且在大多数情况下都能完美工作,所以我现在正试图找到一种更好的方法来完成这个循环。我知道这种循环方法不是做事情的最佳方式,但不确定如何完成任务 我的网页在PEGA Web应用程序中运行,并且本机IE就绪状态指示器总是“就绪”,因此我必须使用Web应用程序的就绪状态标记 有人能帮我吗Vba 有没有更好的方法来完成这个循环?,vba,loops,readystate,Vba,Loops,Readystate,我对VBA很陌生,但我一直在为我的团队制作一个程序。 这段代码大部分时间都有效,但有时会挂起。我不明白为什么它有时会挂起,并且在大多数情况下都能完美工作,所以我现在正试图找到一种更好的方法来完成这个循环。我知道这种循环方法不是做事情的最佳方式,但不确定如何完成任务 我的网页在PEGA Web应用程序中运行,并且本机IE就绪状态指示器总是“就绪”,因此我必须使用Web应用程序的就绪状态标记 有人能帮我吗 Public Sub WaitingForRS() ' FIND THE C360 WINDO
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
在这篇帖子上查看我的答案 @想法5副标题。WaitForLoadSETx()函数有一个手动超时和双循环来帮助捕获状态 这是我用IE对象在VBA中所能做的最好的。理想情况下,您希望学习Selenium、PhantomJS或Puppeter,以便进行实际的浏览器操作。当然也有一些很酷的应用程序,人们已经在这些库的基础上构建了它们来提供帮助(傀儡编剧chrome插件)
或者,为了将内容保存在Excel中,使用一个简单的XMLHTTPRequest对象将浏览器从等式中删除,并单独处理来自web页面的请求/响应。这是一个很好的选择,因为它可以让您专注于以文本形式抓取html内容,而无需使用Javascript或等待页面加载。也许可以尝试使用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
可能需要添加一个时间限制,以便在页面未“就绪”时不会一直循环 不要在错误恢复下一步时用
掩盖错误。当您要做一些可能失败的事情时,请在错误转到0时用尾随的将单行换行。然后对的测试是Nothing
并相应地进行。我没有意识到这不仅仅适用于这一行!显然,我还在学习!谢谢你的建议!虽然我试图理解你的答案中所发生的一切……但我认为,对于我目前的知识水平来说,这有点太复杂了。一旦我这边的事情平静下来,我会仔细看看!非常感谢。