CreateObject 中的 VBA 自动化错误(“InternetExplorer.Application”)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22061833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:52:50  来源:igfitidea点击:

VBA automation error in CreateObject("InternetExplorer.Application")

vbaexcel-vbaexcel

提问by user3305327

I am getting the an automation error while invoking the following object

调用以下对象时出现自动化错误

Set IE = CreateObject("InternetExplorer.Application")

The error is showing

错误显示

Run-time error '-2147467259 (80004005)' Automation error Unspecified error

运行时错误 '-2147467259 (80004005)' 自动化错误未指定的错误

Can anyone have any idea why this is occuring

任何人都可以知道为什么会发生这种情况

'moved code from comments

'从注释中移动代码

Sub TableExample()

    Dim IE As Object
    Dim doc As Object
    Dim strURL As String
    strURL = Range("B2").Value

    Set IE = CreateObject("InternetExplorer.Application")
    With IE '
        .Visible = True
        .navigate Range("B2").Value
        Do Until .readyState = 4
            DoEvents
        Loop
        Do While .Busy
            DoEvents
        Loop
        Set doc = IE.document
        GetAllTables doc
        .Quit
    End With
End Sub

回答by Zachery Poche

I just wasted 4 hours on this, and I'm facepalming at how easy the solution was. Excel creates a new activeX instance every time you run the line:

我只是在这上面浪费了 4 个小时,而我对解决方案的简单性感到震惊。每次运行该行时,Excel 都会创建一个新的 activeX 实例:

Set IE = CreateObject("InternetExplorer.Application")

How exactly that works is out of my league, but those references stick around even after you restart excel. After a couple dozen pile up, excel runs out of memory to make more

它的工作原理超出了我的能力范围,但即使在您重新启动 excel 后,这些参考仍然存在。几十堆后,excel 内存不足,无法制作更多

Restart your computer, (probably an easier way, but that worked for me) and then stick the line

重新启动您的计算机,(可能是一种更简单的方法,但对我有用)然后坚持下去

IE.Quit 

at the end of your code

在你的代码末尾

回答by Tony L.

For others who end up here with the same error...

对于其他最终遇到相同错误的人......

This can also be caused by referencing the Documentobject property in an InternetExplorerobject that has been quit and set to nothing. That this is not what is happening in this question but the following code throws the same error.

这也可能是由于引用已退出并设置为空DocumentInternetExplorer对象中的对象属性引起的。这不是这个问题中发生的事情,但以下代码引发了相同的错误。

Dim ie As New InternetExplorer
ie.Visible = True
ie.Navigate "google.com"

ie.Quit
Set ie = Nothing

If ie.Document Is Nothing Then 'Error thrown here
    MsgBox "Can't get here"
End If