获取InternetExplorer对象的ReadyState时出现自动化错误

Ama*_*mar 6 excel internet-explorer vba excel-vba

我在同一行上得到两个不同的错误.有时候这个:

自动化错误:调用的对象已与其客户端断开连接

而有时:

界面未知

重现错误的最小代码:

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
Run Code Online (Sandbox Code Playgroud)

这需要一个参考:工具>参考...> Microsoft Internet Controls

While ie.ReadyState <> 4第二次发生错误.我该如何解决?

Eli*_*ato 6

这与先前提出的问题重复.问题似乎是由Internet Explorer安全设置引起的 - 当在安全区域之间切换时,IE的当前实例被终止并且创建了新实例,因此您对旧进程的引用不再有效.

一些建议的解决方案是:

  1. 更改IE安全设置.取消选中"Internet选项"的"安全"选项卡上的"启用保护模式".
  2. 直接导航到IP地址而不是URL.这是为我修复它的那个.例如,ie.navigate "64.233.177.106"(Google的IP地址)
  3. Set ie = New InternetExplorerMedium而不是New InternetExplorer.或者在你的情况下,反之亦然.


Jea*_*ett 4

代替

Set ie = New InternetExplorerMedium
Run Code Online (Sandbox Code Playgroud)

只需使用

Set ie = New InternetExplorer
Run Code Online (Sandbox Code Playgroud)

或者,对于后期绑定:

Set ie = CreateObject("InternetExplorer.Application")
Run Code Online (Sandbox Code Playgroud)

这使得错误消失。

我不确定你为什么要InternetExplorerMedium首先使用。引用文档中的小字:

评论

Windows Internet Explorer 8。在 Windows Vista 上,要创建以中等完整性级别运行的 Internet Explorer 实例,请将CLSID_InternetExplorerMedium(在 exdisp.idl 中定义)传递给CoCreateInstance。生成的InternetExplorerMedium对象支持与该对象相同的事件、方法和属性InternetExplorer

您真的在 Windows Vista 上使用 IE8,并且您真的想要“中等完整性级别”(无论这意味着什么)吗?我不这么认为。