Reputation: 93
I created a script that runs through orders in SAP and changes their date. For this, it has to select first all of the items within the order and then proceed to change the date.
The script has several IF statements that deal with expected pop-ups on steps of the process. As you can notice, some of them are exactly the same because it might happen that two pop-up windows identical in structure appear one after the other.
My question is, can I somehow make a list of the IF statements and then ask the scrip to run over it everytime it finds a pop-up window? I tried the OnError GoTo but it seems it looped throughout the list without selecting one of the IF statements.
I hope someone can shed some light on this. Here is my code, the OnError GoTo is inactive:
Sub Roller()
Dim ws As Worksheet
Dim i As Long
Dim maxi As String
Set ws = Excel.ThisWorkbook.Worksheets("Roll")
i = 2
maxi = ws.Cells(Rows.Count, 1).End(xlUp).Row
Do While i < maxi + 1
Set SapGuiAuto = GetObject("SAPGUI") 'Get the SAP GUI Scripting object
Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
Set session = SAPCon.Children(0)
If Cells(4, 10) = "YES" Then
' On Error GoTo PopUpHandler
If Cells(2, 5) = "" Then
session.StartTransaction "va02" 'To directly Open a SAP transaction
session.findById("wnd[0]/usr/ctxtVBAK-VBELN").Text = Cells(i, 1)
session.findById("wnd[0]").sendVKey 0
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
End If
session.findById("wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/btnBT_HEAD").press
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\16").Select
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\16/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9100/ctxtGX_VBAK-VDATU").Text
= Cells(i, 2)
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\16/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9100/ctxtGX_VBAK-ZZWADAT").SetFocus
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\16/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9100/ctxtGX_VBAK-ZZWADAT").caretPosition
= 0
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\17").Select
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
End If
session.findById("wnd[0]").sendVKey 11
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\17/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9200/tblSAPLZV444_PRIMARYTC_RC/ctxtGX_REASON_CODES-REASON_CODE[5,0]").Text
= Cells(i, 4)
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\17/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9200/tblSAPLZV444_PRIMARYTC_RC/ctxtGX_REASON_CODES-REASON_CODE[5,0]").caretPosition
= 4
session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_PCATTA_CREA"
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = Cells(2, 10)
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = Cells(i, 7)
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]").sendVKey 11
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
End If
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/usr/btnZSPOP_PRIMARY-OPTION1").press 'Outstanding issues present
End If
Else
session.StartTransaction "va02" 'To directly Open a SAP transaction
session.findById("wnd[0]/usr/ctxtVBAK-VBELN").Text = Cells(i, 1)
session.findById("wnd[0]").sendVKey 0
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
End If
session.findById("wnd[0]/usr/subSUBSCREEN_HEADER:SAPMV45A:4021/btnBT_HEAD").press
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\16").Select
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\16/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9100/ctxtGX_VBAK-VDATU").Text
= Cells(i, 2)
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\17").Select
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
End If
session.findById("wnd[0]").sendVKey 11
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/usr/btnZSPOP_PRIMARY-OPTION1").press 'Outstanding issues present
End If
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\17/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9200/tblSAPLZV444_PRIMARYTC_RC/ctxtGX_REASON_CODES-REASON_CODE[5,0]").Text
= Cells(i, 4)
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\17/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9200/tblSAPLZV444_PRIMARYTC_RC/ctxtGX_REASON_CODES-TAGGED_ITEMS[6,0]").Text
= Cells(i, 5)
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_HEAD/tabpT\17/ssubSUBSCREEN_BODY:SAPMV45A:4323/subCUSTOMER_SCREEN:SAPLZV444_PRIMARY:9200/tblSAPLZV444_PRIMARYTC_RC/txtGX_REASON_CODES-TAGGED_QTY[7,0]").Text
= Cells(i, 6)
session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_PCATTA_CREA"
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = Cells(2, 10)
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = Cells(i, 7)
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]").sendVKey 11
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
End If
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/usr/btnZSPOP_PRIMARY-OPTION1").press 'Outstanding issues present
End If
End If
Else
' On Error GoTo PopUpHandlerNONFTFE
session.StartTransaction "va02" 'To directly Open a SAP transaction
session.findById("wnd[0]/usr/ctxtVBAK-VBELN").Text = Cells(i, 1)
session.findById("wnd[0]").sendVKey 0
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
End If
session.findById("wnd[0]/usr/tabsTAXI_TABSTRIP_OVERVIEW/tabpT\01/ssubSUBSCREEN_BODY:SAPMV45A:4400/subSUBSCREEN_TC:SAPMV45A:4900/subSUBSCREEN_BUTTONS:SAPMV45A:4050/btnBT_MKAL").press
session.findById("wnd[0]/mbar/menu[1]/menu[1]/menu[3]").Select
session.findById("wnd[1]/usr/ctxtRV45A-S_ETDAT").Text = Cells(i, 2)
session.findById("wnd[1]/usr/ctxtRV45A-S_EZEIT").SetFocus
session.findById("wnd[1]/usr/ctxtRV45A-S_EZEIT").caretPosition = 0
session.findById("wnd[1]/tbar[0]/btn[7]").press
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
End If
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
End If
If session.ActiveWindow.Name = "wnd[2]" Then
session.findById("wnd[2]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
End If
session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_PCATTA_CREA"
session.findById("wnd[1]/usr/ctxtDY_PATH").Text = Cells(2, 10)
session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = Cells(i, 7)
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]").sendVKey 11
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
End If
session.findById("wnd[0]").sendVKey 11
session.findById("wnd[1]/usr/tblSAPLZV01_IBERIATC_OTIFVAL/ctxtX_OTIFVAL-RCODE[3,0]").Text
= Cells(i, 4)
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/tbar[0]/btn[8]").press 'Consider subsequent documents pop-up
End If
If session.ActiveWindow.Name = "wnd[1]" Then
session.findById("wnd[1]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
End If
End If
Range("H" & i) = "RDD updated And attachment added."
i = i + 1
Loop
MsgBox "RDDs have been updated accordingly To the request."
Exit Sub
'PopUpHandler:
'
' If session.ActiveWindow.Name = "wnd[1]" Then
' session.findById("wnd[1]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
' End If
'
' If session.ActiveWindow.Name = "wnd[1]" Then
' session.findById("wnd[1]/usr/btnZSPOP_PRIMARY-OPTION1").press 'Outstanding issues present
' End If
'
' Resume
'PopUpHandlerNONFTFE:
'
' If session.ActiveWindow.Name = "wnd[2]" Then
' session.findById("wnd[2]/tbar[0]/btn[0]").press 'Credit check (maximum % for open items exceeded pop-up
' End If
'
' If session.ActiveWindow.Name = "wnd[2]" Then
' session.findById("wnd[2]/tbar[0]/btn[0]").press 'Consider subsequent documents pop-up
' End If
'
' Resume
End Sub
Upvotes: 0
Views: 522
Reputation: 41
I have found the following error/message handling strategy useful in sapgui. I have some custom functions that get the window count, text message and window title. I then just add new messages and respond as I need to plus a catch all at the end.
Do While guiWindowCount <> 0
msReturnMessage = guiGetWindowText(session.ActiveWindow)
msWndTitle = guiGetWindowTitle
Select Case True
Case InStr(1, msWndTitle, "window title", vbTextCompare)
session.findById("wnd[1]/usr/btnCANCEL").press
msReturnMessage = "Custom error return message"
GoTo FAIL_EXIT
Case InStr(1, msReturnMessage , "message text", vbTextCompare)
session.findById("wnd[1]/tbar[0]/btn[0]").press
GoTo FAIL_EXIT
Case Else
If gbDebugMode then
Stop
Debug.Print msWndTitle, msReturnMessage
Else
msReturnMessage = "Error: " & msWndTitle & "; " & msReturnMessage
GoTo FAIL_EXIT
End If
End Select
Loop
I also use the same startegy to handle status bar messages as follows;
If Not guiStatusBarErrorCheck(msReturnMessage) Then
Select Case True
Case InStr(1, msReturnMessage, "status bar message text", vbTextCompare)
msReturnMessage = "Error: " & msReturnMessage
GoTo FAIL_EXIT
Case Else
If gbDebugMode Then
Stop
Debug.Print msReturnMessage
Else
msReturnMessage = "Error: " & msReturnMessage
GoTo FAIL_EXIT
End If
End Select
End If
Upvotes: 1