-1

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
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Dan2019
  • 1
  • 1
  • 1
  • Your question is unclear, and your case can't be reproduced. You should first debug your script, to know where the issue is. – Sandra Rossi Jul 31 '19 at 20:34

1 Answers1

1

For starters, your code does not mention any transaction to enter in SAP. For example :

session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nme32k" 'Request transaction ME32K
session.findById("wnd[0]").sendVKey 0 'Press 'enter'
session.findById("wnd[0]/usr/ctxtRM06E-EVRTN").Text = Col1 'Outline Agreement number
session.findById("wnd[0]").sendVKey 0 'Press 'enter'

You can simply add your code (other actions) after the if = 'specific status bar text'

ChristofH
  • 11
  • 1