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