自动化错误,Internet Explorer对象Excel VBA出现未指定错误。

4

我已经和一些代码一起工作了一段时间,但突然间它出现了问题。这是我收到的错误信息:

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

这行代码是在实例化InternetExplorer对象时使用的,但我没有更改过代码。它突然停止工作了,可能是什么问题?

这种情况以前也发生过,我通过显式调用库(之前是MSHTML用于HTMLBsaeObject),但现在只是在变量命名时使用一个对象。

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

而设置子程序如下:

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

你能发一下你的代码吗? - Gareth
2个回答

6

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

即:

objIE.Quit

'Put in a brief pause

set objIE = Nothing

我已经将这张表格的所有网络爬虫移植到了C#中,但我肯定会记住这个方法以备将来之需!我曾经认为将对象设置为null就相当于关闭它,但我没有意识到这并没有关闭隐藏的窗口。 - jDave1984

0

对于其他遇到同样错误的人...

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接