word vba: webbrowser not navigating for ReadyState to check

前端 未结 1 483
轮回少年
轮回少年 2020-12-12 06:21

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...

H

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-12 07:04

    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.

    0 讨论(0)
提交回复
热议问题