Internet Explorer 对象 Excel VBA 的自动化错误未指定错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25944187/
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-12 04:36:10  来源:igfitidea点击:

Automation Error Unspecified Error with Internet Explorer Object Excel VBA

excelvba

提问by jDave1984

I have been working with some code for a while and literally, all of the sudden it broke on me. Here is the error message I'm getting:

我一直在使用一些代码,从字面上看,突然间它打破了我。这是我收到的错误消息:

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

the line of code is specifically when instantiating an InternetExporer object, except I have done nothing to change the code. It just stopped working. What could be the issue?

这行代码是专门在实例化 InternetExporer 对象时使用的,除了我没有做任何更改代码。它只是停止工作。可能是什么问题?

This has happened before and I corrected it by explicitly calling the library (MSHTML for a HTMLBsaeObject before), except I'm using just an Object when naming the variables

这以前发生过,我通过显式调用库(之前为 HTMLBsaeObject 使用 MSHTML)来纠正它,除了在命名变量时我只使用一个对象

Public Sub ValueLineResearch()

    setUp
    Dim myFund As String

    loginN = Sheets("Logins").Range("B2").Text
    pwStr = Sheets("Logins").Range("B3").Text

    'Get the site
    iEx.Navigate "https://jump.valueline.com/login.aspx"
    iEx.Visible = True

    Call waitForIE

    'Need to login now don't we
    iEx.Document.forms("aspnetForm").Item("ctl00_ContentPlaceHolder_LoginControl_txtUserID").Value = loginN
    iEx.Document.forms("aspnetForm").Item("ctl00_ContentPlaceHolder_LoginControl_txtUserPw").Value = pwStr
    iEx.Document.forms("aspnetForm").Item("ctl00_ContentPlaceHolder_LoginControl_btnLogin").Click

    Call waitForIE
    Application.Wait DateAdd("s", 6, Now)

    iEx.Navigate "https://research.valueline.com/secure/research#sec=library"

    Call waitForIE

    For Each el1 In iEx.Document.getElementsByClassName("symbol-search textInput ui-autocomplete-input mod_search-symbols primary_symbol_search")
        el1.Value = fundToResearch
    Next

    Application.Wait DateAdd("s", 2, Now)

    iEx.Document.forms("quoteSearch").submit

    Call waitForIE
    Application.Wait DateAdd("s", 2, Now)

    iEx.Navigate "https://research.valueline.com/secure/research#list=recent&sec=company&sym=" & fundToResearch

    Call waitForIE

    'store the Doc
    Set ieDoc = iEx.Document

    'For linkItem = 0 To ieDoc.Links.Length - 1
    '    'Get the PDF
    '    If InStr(1, ieDoc.Links(linkItem).href, ".pdf", vbTextCompare) > 0 And InStr(1, ieDoc.Links(linkItem).href, "UserGuide", vbTextCompare) <= 0 Then
    '        ieDoc.Links(linkItem).Click
    '        iEx.Visible = True
    '        Exit For
    '    End If
    'Next linkItem

    Set iEx = Nothing    

End Sub

And the setup sub is this:

设置子是这样的:

set iEx = CreateObject("InternetExplorer.Application")
fundToResarch = LCase(InputBox("Please Enter a Fund"))

回答by TechBrummie

I've had a similar problem getting the 'Automation Error' creating an instance of InternetExplorer within a function that is called multiple times. It works fine to start, but after a number of calls will crash with the automation error. I discovered that I had multiple IE processes in memory, which means that setting objIE = Nothingis insufficient to remove the process from memory. The solution is to call the 'Quit' method before setting objIE to Nothing.

我遇到了类似的问题,即在多次调用的函数中创建 InternetExplorer 实例时出现“自动化错误”。它可以正常启动,但在多次调用后会因自动化错误而崩溃。我发现我的内存中有多个IE进程,这意味着设置objIE = Nothing不足以从内存中删除进程。解决方案是在将 objIE 设置为 Nothing 之前调用 'Quit' 方法。

That is:

那是:

objIE.Quit

'Put in a brief pause

set objIE = Nothing

回答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 after quitting and setting it to nothing. Note that this is not what is happening in the question. I was able to replicate the error with code similar to this:

这也可能是在退出并将其设置为空后引用Document对象中的对象属性引起的InternetExplorer。请注意,这不是问题中发生的情况。我能够使用类似于此的代码复制错误:

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