本文介绍了word vba:浏览器未导航至 ReadyState 以进行检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从网页中提取链接,但 webbrowser 似乎无法导航,因此我在 webbrowser1.readstate <> readystate_complete... 处无限循环

但是,如果在 webbrowser1.readstate <> readystate_complete 处设置断点,则 webbrowser 在用户表单中成功导航,并且代码可以正常工作....

有什么想法吗?谢谢

直到 n = numWebBrowser1.Navigate URL执行 While WebBrowser1.readyState <>就绪状态_完成环形如果 WebBrowser1.readyState = READYSTATE_COMPLETE 那么'代码万一n = n +1环形
解决方案

您在问题和答案中使用的 while 循环是一个 忙等待 紧密循环,在等待某事发生时徒劳地消耗 CPU 周期.它适用于(某种程度上)InternetExplorer 对象,因为后者在其自己的单独进程中运行.它不适用于进程内 WebBrowser 控件,因为您的循环不会泵送 Windows 消息,这是导航工作所必需的.如果您想坚持使用循环方法,请考虑放置 Sleep 250DoEvents 在您的循环内调用以减轻繁忙的等待和泵送消息.这仍然是不推荐,相反,您应该考虑重构您的使用代码 WebBrowser_DocumentComplete 事件.

I am trying to extract links from webpages, but it seems webbrowser does not navigate, so I get infinite loop at webbrowser1.readstate <> readystate_complete...

HOWEVER, if make a breakpoint at webbrowser1.readstate <> readystate_complete, the webbrowser navigates successfully in the userform, and code works....

Any ideas? Thanks

Do Until n = num

    WebBrowser1.Navigate URL

    Do While WebBrowser1.readyState <> READYSTATE_COMPLETE

    Loop


    If WebBrowser1.readyState = READYSTATE_COMPLETE Then
        'code
    end if

    n = n +1

loop
解决方案

The while loop you use in your question and in your answer is a busy waiting tight loop, consuming CPU cycles in vain while waiting for something to happen. It works (sort of) for InternetExplorer object, because the latter runs in its own separate process. It doesn't work for in-process WebBrowser control, because your loop doesn't pump Windows messages, which is required for navigation to work. If you want to stick with the loop approach, consider placing Sleep 250 and DoEvents calls inside your loop to mitigate busy waiting and pump messages. This is still not recommended, instead you should consider re-factoring your code to use WebBrowser_DocumentComplete event.

这篇关于word vba:浏览器未导航至 ReadyState 以进行检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:09