Goto Error Handling Does Not Work in Loop

Asked

Viewed 2,568 times

0

I have an application running in VBA, in it I have an error treatment that works perfectly the first time the action goes through it, but it is inside a For Each loop and the second time it simply ignores the treatment, I don’t know if I’m missing a detail or if it just doesn’t rotate when called twice on a loop or something, someone knows the reason for it. Below this part of the code that is giving that error.

On Error GoTo Erro
namePdf = doc3.Document.all.tags("font")(8).innerText

For Each exibirDoc In doc2.Document.all.tags("img")

    ver = exibirDoc.src

    If ver = "https://pan.interfile.com.br/imagens/FileSearch.gif" Then
         exibirDoc.Click
         Exit For
    End If

Next
  • 1

    You are forgetting the most important thing, fix the mistake. If it continues there is nothing to do more. There is a great way to help, take the "treatment" of error and let it be shown. https://answall.com/questions/tagged/exce%C3%A7%C3%a3o? Sort=votes&pageSize=50

  • I need him to stay Maniero, I don’t like to use him, but in this case I had to use.

  • 1

    What is the complete code that handles the error? Erro:

  • Elvis, in reality does not have a specific treatment for this error, I just want him to redirect the execution to certain part of the algorithm, so that it continues without crashing the program understands.

  • What’s the part of the code where the most errors occur? And as @Elvis said, you need to do the Erro:, because when the crash occurs, you’re telling the code to jump to Erro:, but there is no code Erro:... Behold in this answer where, in the event of an error, it leaves the Sub, but you can also put a Exit For in case of error. For more information, you have an English tutorial on this

  • 1

    @danieltakeshi I’ll explain better, I have an application that search for certain documents on a specific site, but when the site does not find documents for downloads, it returns me a popup saying it does not have, this is when the program error, And the best way I could figure out how to fix this is to use Goto. But I’ve already found a way to fix this.

Show 1 more comment

3 answers

1

Whenever you use the instruction On Error GoTo to direct to a specific line remember to wipe the error memory using -1 and 0. The code would look like this:

On Error GoTo -1
On Error GoTo 0
On Error GoTo Erro
namePdf = doc3.Document.all.tags("font")(8).innerText

For Each exibirDoc In doc2.Document.all.tags("img")

    ver = exibirDoc.src

    If ver = "https://pan.interfile.com.br/imagens/FileSearch.gif" Then
         exibirDoc.Click
         Exit For
    End If

Next

0


I found a way to solve my problem, played the code snippet that locates the error in a separate Sub, so I call that sub in the code every time the application goes through it, at that point the program is redirected to that sub and when it finishes it goes back to the sub before it and performs the loop, so Goto always works on every lap of For Each. Thank you to everyone who responded, as I said, this is a way to work with Goto more than once in the algorithm. Of course using this method is not well regarded, but it was the only way I found to work on my problem.

-1

Good solution to "clean the memory" with 0 and -1, however just forgot to mention about instruction On Error Goto -1 On Error Goto 0 On Error Goto Error has to be inside the loop...

At least it was the way it worked here with me.

Valeu Hugs...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.