excel VBA代码在步入时有效,但在运行时无效

我有以下行,当我现场运行时失败,但当我在此行之前打破应用程序时,它工作正常.

I = InStr(html_document.body.innerHTML, "EPG")

我很长,被初始化了,应该可以正常工作.
html_document是IE.document,通过初始化

Set IE = New InternetExplorer

在此行之前设置断点使其工作,我选择正确的字符位置.

在此行之后放置一个断点,我保持为0.

任何想法都会非常感激.

最佳答案 您最好使用READYSTATE枚举来检查页面是否已实际加载.

'READYSTATE_UNINITIALIZED = 0
'READYSTATE_LOADING = 1
'READYSTATE_LOADED = 2
'READYSTATE_INTERACTIVE = 3
'READYSTATE_COMPLETE = 4


Dim IE As New InternetExplorer
'// your other code here
IE.Navigate "http://www.google.co.uk"

While IE.ReadyState <> READYSTATE_COMPLETE
    DoEvents
Wend

'// Following code will only run once the document has completely loaded.

Set html_document = IE.Document
I = InStr(html_document.body.innerHTML, "EPG")

'// rest of your code here....
点赞