我如何解决这个“运行时错误'-2147023706(800704a6)”?

时间:2017-01-04 14:52:04

标签: excel vba excel-vba runtime-error

我的代码存在很大问题:

Sub EstrazDati_A_ieri()

Dim doc As HTMLDocument
Dim output As Object

Set IE = New InternetExplorer
IE.Visible = False
IE.navigate Range("D2")

Do
    DoEvents
    Loop Until IE.readyState = READYSTATE_COMPLETE

Set doc = IE.document
Set output = doc.getElementsByTagName("a")

For Each link In output
    If link.innerHTML = "Main" Then
        Range("D3").Value2 = link
    End If

Next
IE.Quit

End Sub

代码是正确的并且可以正常运行,但有时在连续使用自身后,Windows框中会出现运行时错误:

Set IE = New InternetExplorer

我已经测试过在互联网上找到的几种解决方案,但我无法修复它。

P.S。我是一个意大利男孩,对不好的英语抱歉:)

2 个答案:

答案 0 :(得分:0)

我现在在互联网上找到了这个解决方案,它可以正常工作,您认为这是对的吗?

Sub EstrazDati_A_ieri()
On Error Resume Next

Dim doc As HTMLDocument
Dim output As Object

Set IE = New InternetExplorer
IE.Visible = False
IE.navigate Range("D2")

Do
    DoEvents
    Loop Until IE.readyState = READYSTATE_COMPLETE

Set doc = IE.document
Set output = doc.getElementsByTagName("a")

For Each link In output
    If link.innerHTML = "Main" Then
        Range("D3").Value2 = link
    End If

Next
IE.Quit
Set IE = Nothing
End Sub

编辑:现在我已经尝试过这种方式了

Sub EstrazDati_A_ieri()
Dim IE As Object
Dim doc As HTMLDocument
Dim output As Object

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
IE.navigate Range("D2")

Do
    DoEvents
    Loop Until IE.readyState = READYSTATE_COMPLETE

Set doc = IE.document
Set output = doc.getElementsByTagName("a")

For Each link In output
    If link.innerHTML = "Main" Then
        Range("D3").Value2 = link
    End If

Next
IE.Quit
Set IE = Nothing
End Sub

但是在这些宏观错误的第八次运行之后:(

答案 1 :(得分:0)

Sub EstrazDati_A_ieri()
Dim IE As Object
Dim doc As HTMLDocument
Dim output As Object

Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.navigate Range("D2")

Do
    DoEvents
    Loop Until IE.readyState = READYSTATE_COMPLETE

Set doc = IE.document
Set output = doc.getElementsByTagName("a")

For Each link In output
    If link.innerHTML = "Main" Then
        Range("D3").Value2 = link
    End If

Next

End Sub

好的伙计们!以这种方式没有错误。问题只是我不会每次打开一个新的网页,但我会使用相同的,因为我必须重复运行这个宏12次。 我该如何修改这行代码?

Set IE = CreateObject("InternetExplorer.Application")