I am trying to create a script to enter transactions in SAP from an Excel document. Occasionally some transactions will need a different action performed, which I delineated in my code below.
I have tried else
, else if
, if
, and if not
conditions. I anticipate my syntax is incorrect somewhere.
I would like my VBS to keep running and perform one action or the other based on if "Order cannot be delivered (see long text)" prints at the bottom of SAP until there are no more rows on the spreadsheet.
Any assistance or advice is very much appreciated.
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
session.findById("wnd[0]").maximize
Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(,"Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
For i = 2 to objSheet.UsedRange.Rows.Count
COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'ColumnA
COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'ColumnB
COL3 = Trim(CStr(objSheet.Cells(i, 3).Value)) 'ColumnC
COL4 = Trim(CStr(objSheet.Cells(i, 4).Value)) 'ColumnD
session.findById("wnd[0]/usr/ctxtLIKP-VSTEL").text = COL1
session.findById("wnd[0]/usr/ctxtLV50C-DATBI").text = COL3
session.findById("wnd[0]/usr/ctxtLV50C-VBELN").text = COL2
session.findById("wnd[0]/usr/ctxtLV50C-VBELN").caretPosition = 7
session.findById("wnd[0]/tbar[0]/btn[0]").press
' If sbar prints below message
If Session.FindById("wnd[0]/sbar").Text <> "Order cannot be delivered (see long text)" Then ...
session.findById("wnd[0]/tbar[0]/btn[3]").press
' Else save SAP transaction and restart until conclusion of spreadsheet
Else
session.findById("wnd[0]/tbar[0]/btn[11]").press
End If
Next