VBA:尝试访问网站,但网站偶尔无法加载

VBA:尝试访问网站,但网站偶尔无法加载,vba,excel,navigateurl,Vba,Excel,Navigateurl,目前的代码如下: Set IE = CreateObject("InternetExplorer.Application") IE.Navigate "https://www.website.com/" Application.StatusBar = "https://www.website.com/ is loading. Please wait..." Do While IE.Busy Application.Wait DateAdd("s", 1, Now) Loop Ap

目前的代码如下:

Set IE = CreateObject("InternetExplorer.Application")
IE.Navigate "https://www.website.com/"
Application.StatusBar = "https://www.website.com/ is loading. Please wait..."
Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
    Loop
Application.StatusBar = "Search form submission. Please wait..."
我遇到的问题是“”无法不时加载(我相信这是由于我反复访问它)。结果是代码永远不会超出此循环:

Do While IE.Busy
    Application.Wait DateAdd("s", 1, Now)
    Loop
在我看来,一个合乎逻辑的解决方案是在达到某个时间限制(30秒?)后,杀死IE并重新启动整个过程。请告知如何对该解决方案进行编码,和/或如何将更有效的解决方案编码在一起。谢谢你!如果有必要的话,我正在使用IE10

此外,我还发现了一个链接,其中包含一些相关信息:
您可以尝试修改代码以读取

Sub LoadWebsite()
Dim iSecondCounter As Integer
Static iAttempts As Integer

iAttempts = iAttempts + 1

Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "https://www.website.com/"
Application.StatusBar = "https://www.website.com/ is loading. Please wait..."

iSecondCounter = 0
Do While ie.Busy And iSecondCounter < 30
    Application.Wait DateAdd("s", 1, Now)
    iSecondCounter = iSecondCounter + 1
Loop
If iSecondCounter = 30 Then
    ie.Quit
    If iAttempts <= 3 Then Call LoadWebsite
End If

Set ie = Nothing
End Sub
Sub-LoadWebsite()
Dim IseCond计数器为整数
静态IAT尝试为整数
IATENTS=IATENTS+1
设置ie=CreateObject(“InternetExplorer.Application”)
即“导航”https://www.website.com/"
Application.StatusBar=”https://www.website.com/ 正在加载。请稍候…”
等秒计数器=0
在忙的时候做,等计数器<30
Application.Wait DateAdd(“s”,1,Now)
iSecondCounter=iSecondCounter+1
环
如果iSecondCounter=30,则
即退出

如果IAT尝试您可以尝试修改代码以读取

Sub LoadWebsite()
Dim iSecondCounter As Integer
Static iAttempts As Integer

iAttempts = iAttempts + 1

Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "https://www.website.com/"
Application.StatusBar = "https://www.website.com/ is loading. Please wait..."

iSecondCounter = 0
Do While ie.Busy And iSecondCounter < 30
    Application.Wait DateAdd("s", 1, Now)
    iSecondCounter = iSecondCounter + 1
Loop
If iSecondCounter = 30 Then
    ie.Quit
    If iAttempts <= 3 Then Call LoadWebsite
End If

Set ie = Nothing
End Sub
Sub-LoadWebsite()
Dim IseCond计数器为整数
静态IAT尝试为整数
IATENTS=IATENTS+1
设置ie=CreateObject(“InternetExplorer.Application”)
即“导航”https://www.website.com/"
Application.StatusBar=”https://www.website.com/ 正在加载。请稍候…”
等秒计数器=0
在忙的时候做,等计数器<30
Application.Wait DateAdd(“s”,1,Now)
iSecondCounter=iSecondCounter+1
环
如果iSecondCounter=30,则
即退出

如果I尝试确定,您似乎在问一个关于获取HTML元素集合的新问题,因此我将把它作为新答案发布。出现对象错误的原因似乎是您尚未将objCollection标注为正确的对象。为了做到这一点,我修改了原始答案中的代码,并将集合设置为代码片段中的集合。要使用这段代码,我必须包含对MicrosoftHTML对象库的引用

请注意,这段代码在退出sub之前会破坏元素集合,您可能想对它做些什么

Sub LoadWebsite()
Dim iSecondCounter As Integer
Static iAttempts As Integer
Dim objCollection As IHTMLElementCollection
iAttempts = iAttempts + 1

Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "https://www.website.com/"
Application.StatusBar = "https://www.website.com/ is loading. Please wait..."

iSecondCounter = 0
Do While ie.Busy And iSecondCounter < 30
    Application.Wait DateAdd("s", 1, Now)
    iSecondCounter = iSecondCounter + 1
Loop
If iSecondCounter = 30 Then
    ie.Quit
    If iAttempts <= 3 Then Call LoadWebsite
End If


Set objCollection = (ie.Document.getElementsByTagName("INPUT"))

Cleanup:

Set ie = Nothing
Set objCollection = Nothing

End Sub
Sub-LoadWebsite()
Dim IseCond计数器为整数
静态IAT尝试为整数
作为IHTMlementCollection的Dim对象集合
IATENTS=IATENTS+1
设置ie=CreateObject(“InternetExplorer.Application”)
即“导航”https://www.website.com/"
Application.StatusBar=”https://www.website.com/ 正在加载。请稍候…”
等秒计数器=0
在忙的时候做,等计数器<30
Application.Wait DateAdd(“s”,1,Now)
iSecondCounter=iSecondCounter+1
环
如果iSecondCounter=30,则
即退出

如果I尝试确定,您似乎在问一个关于获取HTML元素集合的新问题,因此我将把它作为新答案发布。出现对象错误的原因似乎是您尚未将objCollection标注为正确的对象。为了做到这一点,我修改了原始答案中的代码,并将集合设置为代码片段中的集合。要使用这段代码,我必须包含对MicrosoftHTML对象库的引用

请注意,这段代码在退出sub之前会破坏元素集合,您可能想对它做些什么

Sub LoadWebsite()
Dim iSecondCounter As Integer
Static iAttempts As Integer
Dim objCollection As IHTMLElementCollection
iAttempts = iAttempts + 1

Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "https://www.website.com/"
Application.StatusBar = "https://www.website.com/ is loading. Please wait..."

iSecondCounter = 0
Do While ie.Busy And iSecondCounter < 30
    Application.Wait DateAdd("s", 1, Now)
    iSecondCounter = iSecondCounter + 1
Loop
If iSecondCounter = 30 Then
    ie.Quit
    If iAttempts <= 3 Then Call LoadWebsite
End If


Set objCollection = (ie.Document.getElementsByTagName("INPUT"))

Cleanup:

Set ie = Nothing
Set objCollection = Nothing

End Sub
Sub-LoadWebsite()
Dim IseCond计数器为整数
静态IAT尝试为整数
作为IHTMlementCollection的Dim对象集合
IATENTS=IATENTS+1
设置ie=CreateObject(“InternetExplorer.Application”)
即“导航”https://www.website.com/"
Application.StatusBar=”https://www.website.com/ 正在加载。请稍候…”
等秒计数器=0
在忙的时候做,等计数器<30
Application.Wait DateAdd(“s”,1,Now)
iSecondCounter=iSecondCounter+1
环
如果iSecondCounter=30,则
即退出

如果i尝试也许您可以添加代码来帮助您等待加载,X秒后您可以决定是否等待,或者您可以优雅地退出/执行其他操作(发布消息等)


也许您可以添加代码来帮助您等待加载,X秒后您可以决定是否等待,或者您可以优雅地退出/执行其他操作(发布消息等)


宏执行完代码后,您是否将IE设置为空?是的,先生!在代码末尾进行了大量清理。宏执行完代码后,您是否将
IE
设置为空?是的,先生!代码末尾有很多清理工作。谢谢你。那么,如果IE被杀死,我如何让它有条件地循环回呢?嗨,我已经编辑了代码,使其成为一个递归子例程,如果IE失败并被杀死,子例程将再次运行,但如果在3次之后它仍然没有加载,它将放弃(将您从无限循环中解救出来)。因此,代码现在是:
调用LoadWebsite Set-objCollection=IE.document.getElementsByTagName(“输入”)
并导致此错误:未设置对象变量或With block变量。您是否使用create Object设置IE对象?它不会从该子对象中结转?谢谢。那么,如果IE被杀死,我如何让它有条件地循环回呢?嗨,我已经编辑了代码,使其成为一个递归子例程,如果IE失败并被杀死,子例程将再次运行,但如果在3次之后它仍然没有加载,它将放弃(将您从无限循环中解救出来)。因此,代码现在为:
调用LoadWebsite Set-objCollection=IE.document.getElementsByTagName(“输入”)
并导致此错误:未设置对象变量或With block变量。您是否使用create Object设置IE对象?它未从此子对象结转?