vba字:Webbrowser无法导航到ReadyState进行检查 [英] word vba: webbrowser not navigating for ReadyState to check
问题描述
我正在尝试从网页中提取链接,但是似乎webbrowser无法导航,所以我在webbrowser1.readstate出现无限循环<> readystate_complete ...
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...
但是,如果在webbrowser1.readstate<> readystate_complete处设置断点,则Webbrowser会在用户窗体中成功导航,并且代码可以正常工作....
HOWEVER, if make a breakpoint at webbrowser1.readstate <> readystate_complete, the webbrowser navigates successfully in the userform, and code works....
有什么想法吗?谢谢
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
推荐答案
在问题和答案中使用的while
循环是睡眠250 和不推荐,相反,您应该考虑重构您的使用 WebBrowser_DocumentComplete
事件的代码
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.
这篇关于vba字:Webbrowser无法导航到ReadyState进行检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!