How to use Err.Clear correctly

Asked

Viewed 474 times

0

Good afternoon, everyone.

This is my first question here in the forum, so I ask forgiveness if it is not easy to understand my doubt.

Next, I have one script that automates nfs approvals on SAP, but inside the looping once it has finished approving, it will no longer find the "ID" of the object to click generating the error.

I made a "On error goto Feito" where once you enter there runs the "Err.Clear" and appears a msgbox asking if the error is of position, if it is yes, it runs from another part of the code, and so continues the looping, but even with the "Err.Clear" it does not clear the last error by making it impossible for the MsgBox for the user to check the error and thus give continuation to the script, the error will always be the same as it will not find the "ID" of the object. In short, I wanted to know how I do so that whenever the error occurs, it clears the last given error, and as soon as it happens again, instead of stopping to debug, it goes again to msgbox and continue the looping.

The code is a sub, I’ll put some snippets to illustrate:

Sub DL_ZNFE_MAIN()
On Error GoTo Feito:
Dim resultado As VbMsgBoxResult
var_sap_session.findById("wnd[0]/tbar[0]/okcd").Text = "znfe" 
var_sap_session.findById("wnd[0]").sendVKey 0

This is the first chunk, basically it will enter a transaction, put some inputs, and done this will appear several notes, which will be accessed by this code:

For i = 0 To 30


var_sap_session.findById("wnd[0]/usr/tblZKFBC_MONITOR_NFETC_SINTESE/txtT_SINTESE-DOCNUM[1,0]").SetFocus
var_sap_session.findById("wnd[0]/usr/tblZKFBC_MONITOR_NFETC_SINTESE/txtT_SINTESE-DOCNUM[1,0]").caretPosition = 0
var_sap_session.findById("wnd[0]").sendVKey 2
var_sap_session.findById("wnd[0]/usr/btnSWE19").press
var_sap_session.findById("wnd[0]/tbar[1]/btn[8]").press

But when he makes a mistake, he comes to this part:

Feito:
Err.Clear
resultado = MsgBox("O erro é de posição?", vbYesNo, "Erro")
If resultado = vbYes Then
    var_sap_session.findById("wnd[2]/usr/lbl[43,3]").SetFocus
    var_sap_session.findById("wnd[2]/usr/lbl[43,3]").caretPosition = 3
    var_sap_session.findById("wnd[2]").sendVKey 2
    var_sap_session.findById("wnd[0]").sendVKey 2
    GoTo Continue
Else
Err.Clear
var_sap_session.findById("wnd[0]/tbar[1]/btn[6]").press
MsgBox "Não foi possivel encontrar notas rejeitadas."

End If

Then after arriving at the error, the msgbox will appear that will ask if the error is of position or not, if yes it will go to the "Continue":

Continue:
    var_sap_session.findById("wnd[1]/usr/sub:SAPLSPO4:0300/txtSVALD-VALUE[2,21]").Text = "0.00"
    var_sap_session.findById("wnd[1]/usr/sub:SAPLSPO4:0300/txtSVALD-VALUE[2,21]").SetFocus
    var_sap_session.findById("wnd[1]/usr/sub:SAPLSPO4:0300/txtSVALD-VALUE[2,21]").caretPosition = 4
    var_sap_session.findById("wnd[1]/tbar[0]/btn[0]").press

And so will continue the looping inside this FOR, but if by any chance it gives error again, instead of generating the msgbox again, it already for the code.

  • Hello @Vittor. Try to structure your question better, it is quite confusing.

  • And enter the code of what you have accomplished, whether it is a Sub or Function or event. Creating a [mcve] helps understanding

  • @danieltakeshi added some lines of code from my sub, I hope it was clearer, thank you very much!

1 answer

0

Hello, try the following command to "reset" the errors at the beginning of the loop:

On Error GoTo -1

Abs!

Browser other questions tagged

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