如何使VBA等待IE页面加载? [英] How do I make VBA wait for IE page to load?

查看:130
本文介绍了如何使VBA等待IE页面加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过VBA自动化IE,但似乎无法获得VBA代码来等到页面加载后再抓取文档.由于我的代码过快,这导致了各种各样的失败.如何使VBA等待IE页面加载?我尝试过的许多方法都是在stackoverflow上找到的,但似乎没有一种方法对我有用.

I am automating IE through VBA and I can't seem to get the VBA code to wait until the page is loaded before grabbing the document. This is leading to all sorts of failures with my code being too fast. How can I get VBA to wait for the IE page to load? Many of the methods I have tried I have found on stackoverflow but none seem to work for me.

ie 是我的 InternetExplorer 对象.

我尝试了,但无济于事:

I have tried, to no avail:

Do
    If ie.readyState = 4 Then
        Exit Do
    Else
        DoEvents
    End If
Loop

也:

Do While ie.Busy Or Not ie.ReadyState = READYSTATE_COMPLETE
    DoEvents
Loop

也:

Do 
    DoEvents
Loop Until ie.ReadyState = READYSTATE_COMPLETE

我什至尝试:

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

...

Do
    Sleep 250
Loop Until ie.readyState = READYSTATE_COMPLETE

这些都不起作用,即使页面尚未完成加载,代码也会立即跳过循环并尝试获取页面的html文档.

None of these have worked, the code skips over the loop immediately and tries to grab the html document of the page even though the page has not finished loading.

我正在导航到登录页面,填写用户名和密码,然后单击登录.登录后,我需要更改一些字段(下拉列表和文本框).更改这些字段后,我使用VBA单击按钮以保存设置,然后导航到另一个页面.该代码未正确设置字段,因为它跳过了等待页面加载的等待,因此我使用错误的设置输入了代码的下一部分.

I am navigating to a login page, filling in a username and password, and clicking log-in. After I log-in I need to change some fields (drop downs and text boxes). After I change these fields I use VBA to click a button to save the settings and then navigate to another page. The code does not properly set the fields because it skips past waiting for the page to load, thus I enter the next part of my code with incorrect settings.

我想知道这是否可能是由于ajax请求引起的?我什至不知道该怎么讲.

I am wondering if this is possibly due to an ajax request? I don't know how to even tell.

在抓取html文档之前,有人能可靠地等待页面加载到IE中吗?

Does anyone have a reliable means of waiting for a page to load in IE before grabbing the html document?

我知道代码正在执行我想要的操作,因为如果我手动浏览它并等待页面加载,它的行为将与预期的一样.

I know the code is doing what I want because if I manually step through it and wait for the page to load, it behaves as expected.

如有必要,我可以添加更多说明.

I can add more clarification if necessary.

推荐答案

您可以等待

You could wait for the DocumentComplete event to fire. But for that you need to have the browser control placed on a worksheet. In the code of that worksheet, enter this code (assuming you named the browser control ie):

Private docComplete As Boolean

Private Sub ie_DocumentComplete(ByVal pDisp As Object, url As Variant)
    docComplete = True
End Sub

Public Sub NavigateAndWait(url As String)
    docComplete = False
    ie.Navigate url
    Do
        DoEvents
    Loop Until docComplete
End Sub

Sub Main()
    ' Example use
    NavigateAndWait "www.google.com"
    MsgBox "Ready"
End Sub

通过将 NavigateAndWait 设置为 Public Sub ,您可以从可能具有的其他模块中调用它.最好将需要 ie 的代码移到此工作表的模块中.请注意,浏览器控件必须是可见的,并且在活动工作表上才能触发DocumentComplete事件.

By making NavigateAndWait a Public Sub you can call it from other modules you might have. It is best to move code that needs ie into this sheet's module. Note that the browser control must be visible and on the active sheet for the DocumentComplete event to fire.

这篇关于如何使VBA等待IE页面加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆