word vba: webbrowser not navigating for ReadyState to check

风流意气都作罢 提交于 2019-11-27 08:43:56

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/19012356/word-vba-webbrowser-not-navigating-for-readystate-to-check

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!