Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

vba - Automation error when getting ReadyState of InternetExplorer object

I get two different errors on the same line. Sometimes this one:

Automation error: object invoked has disconnected from its clients

and sometimes:

the interface is unknown

Minimal code to reproduce error:

Sub mcve()
    Dim ie As Object
    Dim www As String
    Set ie = New InternetExplorerMedium
    www = "http://www.stackoverflow.com"
    ie.navigate www
    ie.Visible = False
    While ie.ReadyState <> 4    ' <~~~~~~~~~~~~~~~~~~~~~~~~ Error occurs here
        DoEvents
    Wend
End Sub

This requires a reference: Tools > References... > Microsoft Internet Controls

The error occurs on While ie.ReadyState <> 4 the second time. How do I fix this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a duplicate of a previously asked question. The problem seems to be caused by Internet Explorer security settings - when switching between security zones, the current instance of IE is killed and a new instance is created, so your reference to the old process is no longer valid.

Some of the suggested solutions were:

  1. Change IE security settings. Uncheck "enable protected mode" on the Security tab of Internet Options.
  2. Navigate to the IP address directly instead of the URL. This is the one that fixed it for me. For example, ie.navigate "64.233.177.106" (Google's IP address)
  3. Set ie = New InternetExplorerMedium instead of New InternetExplorer. Or in your case, vice versa.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...