0

I'm working with excel VBA for this project.

I'm try to run a scrip on sap that will leads to differents patches depending of what kinda of error appears on the currently loop and to do that i'm using on erro goto, because those different patches will only be an option if the currently patch face one error.

I will highlight some parts that for some reason break the code and also don't go to the patch that i'm saying to go if a error appear.

The first error that appears it's on this part of the code:

Pula_popup01:
            
            **Session.findById("wnd[1]/usr/btnSPOP-VAROPTION1").press
    
            On Error GoTo -1
            On Error GoTo Pula_popup02**

For some reason if this bottom don't exist on sap to be press should lead us to the next step how is "Pula_popup02" but that doesn't happen and the code breaks.

Here it's the full code:

Sub SWO_Closed()

Set SapGUIAuto = GetObject("SAPGUI")
Set SAPApp = SapGUIAuto.GetScriptingEngine
Set SAPCon = SAPApp.Children(0)
Set Session = SAPCon.Children(0)

'
If IsObject(WScript) Then
WScript.ConnectObject Session, "on"
WScript.ConnectObject Application, "on"
End If

    Dim objExcel
    Dim objSheet, intRow, i
    Set objExcel = GetObject(, "Excel.Application")
    Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
     
    
    For i = 2 To objSheet.UsedRange.Rows.Count
Err.Clear
    
    SWO = Trim(CStr(objSheet.Cells(i, 1).Value))
    SWO_Status = Trim(CStr(objSheet.Cells(i, 2).Value))
    Fechada = Trim(CStr(objSheet.Cells(i, 3).Value))
    Status = ""
    
    If SWO = "" Then

    MsgBox "Macro finalizada!"
    Exit Sub
    
    End If
    
        If SWO_Status = "Fechar" Then
    
            'Session.findById("wnd[0]").maximize
            Session.findById("wnd[0]/tbar[0]/okcd").Text = "/niw32"
            Session.findById("wnd[0]").sendVKey 0
            Session.findById("wnd[0]/usr/ctxtCAUFVD-AUFNR").Text = SWO
            Session.findById("wnd[0]").sendVKey 0
            Session.findById("wnd[0]/tbar[1]/btn[48]").press
            
            On Error GoTo -1
            On Error GoTo Pula_popup01
            
            
Pula_popup01:
            
            **Session.findById("wnd[1]/usr/btnSPOP-VAROPTION1").press
    
            On Error GoTo -1
            On Error GoTo Pula_popup02
            
            'if no error appears
            GoTo resume_fim_loop
            
Pula_popup02:
            
            Session.findById("wnd[1]/usr/btnOPTION2").press
            Session.findById("wnd[1]/tbar[0]/btn[0]").press
            On Error GoTo -1
            On Error GoTo Pula_popup03
            
            Session.findById("wnd[1]/usr/btnSPOP-VAROPTION1").press
    
            'if no error appears
            GoTo resume_fim_loop
            
Pula_popup03:

            Session.findById("wnd[2]/tbar[0]/btn[0]").press
            Session.findById("wnd[1]/usr/btnSPOP-VAROPTION1").press
            
            'the last hope of mankind
            GoTo resume_fim_loop

Continue:

            Session.findById("wnd[1]/usr/btnOPTION2").press
            Session.findById("wnd[1]/tbar[0]/btn[0]").press
            Status = "Ok"
            
                          
        
        End If
        
resume_fim_loop:
    
        If Status = "Ok" Then
        
            Cells(i, 3).Value = "SWO Fechada"
        
            
        ElseIf Status = "" Then
        
            Cells(i, 3).Value = "Erro ao Fechar"
        
        End If
     
        
    Next
   
    
MsgBox ("Macro finalizada!")

End Sub
  • 1
    Your error handling is wrong. If an error occurs in the first line after the `Pula_popup01` label, then the code will jump back to that label (as you set it as the error handler) and then repeat the same line of code that just errored. – Rory Mar 04 '21 at 12:05
  • You tagged your question `VBA`, but you're using `On Error GoTo -1` which is used in [Visual Basic](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/on-error-statement), but not in [VBA](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/on-error-statement). – VBasic2008 Mar 04 '21 at 12:21
  • @VBasic2008 Nope, it's allowed in VBA. See for example https://stackoverflow.com/questions/14158901/difference-between-on-error-goto-0-and-on-error-goto-1-vba – FunThomas Mar 04 '21 at 12:25
  • How can i correct my error handling? can you help me with that? – Jonathan Curcovezki Mar 04 '21 at 14:21
  • Show me an example? – Jonathan Curcovezki Mar 04 '21 at 14:22
  • 1
    All you need to know is here: http://www.cpearson.com/excel/ErrorHandling.htm – Tim Williams Mar 04 '21 at 17:20

0 Answers0