错误转到:多次

时间:2014-01-06 19:25:15

标签: vba excel-vba excel

我正在使用宏从网站上获取信息:

Sub get_prices()

Dim IE As Object
Dim URL As String
Dim cell As Range

Dim rng As Range
Set rng = Range("A2", Range("A1048576").End(xlUp))

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

For Each cell In rng.Cells
comeco:
    On Error GoTo pau:
    URL = Cells(1, 2) & cell.Text & "?utm_source=teste" & Rnd
    IE.Navigate (URL)
    Do
        DoEvents
    Loop Until IE.READYSTATE = 4
    Application.Wait (Now + TimeValue("00:00:05"))

    Cells(cell.Row, 2) = Mid(IE.Document.GetElementById("ctl00_Conteudo_ctl01_precoPorValue").innertext, 3)
    Cells(cell.Row, 2).Formula = _
                WorksheetFunction.Substitute(WorksheetFunction.Substitute(Cells(cell.Row, 2), ".", ""), ",", ".")
Next

pau:
IE.Quit
Set IE = Nothing
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
GoTo comeco:

End Sub

问题是,我的Excel有时会“丢失IE对象”,并且在以下行中收到错误消息“THE OBJECT IS REQUERIED”: Cells(cell.Row, 2) = Mid(IE.Document.GetElementById("ctl00_Conteudo_ctl01_precoPorValue").innertext, 3)

通常,我只是调试代码,并创建一个新行,如: Set IE = CreateObject("InternetExplorer.Application") 然后我恢复了宏。

Bud我想做一些更健壮的东西,每当发生错误时,它会关闭IE,然后重新打开它。

我正在尝试使用 on error goto 但它只在第一次发生错误时起作用,如果它再次发生则不会转到错误部分。

任何人都可以帮助我吗?

4 个答案:

答案 0 :(得分:5)

如果您尝试添加错误处理以捕获缺少的InternetExplorer实例,请尝试以下操作。在违规行之前和之后的代码中添加错误语句。

On Error Goto IE_missing   'This changes error handling from the default popup error message to instead direct VBA to jump to the IE_missing label and try the code there.
Cells(cell.Row, 2) = Mid(IE.Document.GetElementById("ctl00_Conteudo_ctl01_precoPorValue").innertext, 3)
On Error Goto 0   'This resets the error handling back to the default so you don't accidentally catch errors in other parts of the code

然后在代码的末尾放置一个部分,如下所示:

    Exit Sub  'This keeps the rest of the Sub's code from accidentally running down into the error section.

IE_missing:   'When the specific line above errors, VBA directs execution here
    Set IE = CreateObject("InternetExplorer.Application") 
    Resume    'Now that the IE instance has been set again, retry the offending line of code

答案 1 :(得分:1)

我不确定为什么Excel“有点丢失IE对象”,所以我不愿意给出这个答案。

为什么不在调用之前添加一个检查以确保IE对象不为null:

If IE Is Nothing Then
  Set IE = CreateObject("InternetExplorer.Application")
End If
Cells(kit.Row, kit.Column + i) = Mid(IE.Document.GetElementById("ctl00_Conteudo_ctl01_precoPorValue").innertext, 3)

答案 2 :(得分:1)

调用导航时会丢失IE窗口。我通过调用导航然后将我正在寻找的url传递给下面的函数并像这样调用它来克服这个

'this is where you create your URL
URL = Cells(1, kit.Column + i) & kit.Text & "?utm_source=teste" & Rnd
'navigate to the page
IE.Navigate (URL)
'get the page we just went to
Set IE = GetWebPage(URL)






''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Desc: The Function gets the Internet Explorer window that has the current
'   URL from the sURL Parameter.  The Function Timesout after 30 seconds
'Input parameters:
    'String sURL - The URL to look for
'Output parameters:
    'InternetExplorer ie - the Internet Explorer window holding the webpage
'Result: returns the the Internet Explorer window holding the webpage
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function GetWebPage(sUrl As String) As InternetExplorer
Dim winShell As Shell
Dim dt As Date
dt = DateAdd("s", 300, DateTime.Now)

Dim ie As InternetExplorer

Do While dt > DateTime.Now
    Set winShell = New Shell
    'loop through the windows and check the internet explorer windows
    For Each ie In winShell.Windows
    If ie.LocationURL = sUrl Then

    ie.Visible = True
    ie.Silent = True
        Set GetWebPage = ie

        Do While ie.Busy
        DoEvents
        Loop
        Exit Do

    Set winShell = Nothing
    End If
    Next ie
    Set winShell = Nothing
    DoEvents
Loop
End Function

答案 3 :(得分:0)

Mid(IE.Document.GetElementById(" ctl00_Conteudo_ctl01_precoPorValue")。innertext,3, xx

中间必须有xx编号