0

I would like to use For Each Loop for SAP transaction, but my list of orders contain variables that are non-existent in the transaction, thus bringing an error.

How do I fix this For Each Loop so that it completely ignores the error and just moves to the next item on the list?

The code doesn't accept my "Next" command after If.

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

'Variables
Dim order(2)

order(0)="xxx"
order(1)="yyy"
order(2)="zzz"

For Each x In order
    
    On Error Resume Next
    If err.number <> 0 Then
        Next
    Else
        session.findById("wnd[0]").maximize
        session.findById("wnd[0]/usr/ctxtRC29L-MATNR").text = x
        session.findById("wnd[0]/usr/ctxtRC29L-MATNR").caretPosition = 8
        session.findById("wnd[0]/tbar[1]/btn[8]").press
        session.findById("wnd[0]/tbar[1]/btn[45]").press
        session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").select
        session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").setFocus
        session.findById("wnd[1]/tbar[0]/btn[0]").press

        'Path
        session.findById("wnd[1]/usr/ctxtDY_PATH").text = "C:\Users\blabla"

        'Order name
        session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = x + ".xls"
        session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 12
        session.findById("wnd[1]/tbar[0]/btn[0]").press
        session.findById("wnd[0]/tbar[0]/btn[3]").press
    End If
    On Error Goto 0
Next

Thanks to anyone interested!

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Aleksi
  • 1
  • Unfortunately no – Aleksi Jan 16 '23 at 14:17
  • I'm new to VBScripting. Would you care to explain what I'm supposed to implement from this post? – Aleksi Jan 16 '23 at 15:36
  • 1
    You want to implement a "continue" (to the next iteration of the loop) which doesn't exist in VBA. This post gives some workarounds both in the question and in the answer. In both solutions, the trick is to nest a dummy one-time loop (so it's not really a loop) starting right after the start of the concerned loop and finishing right before the end of this same loop, and use an `Exit For` (mentioned in the question) or an `Exit Do` (mentioned in the answer). NB: someone correctly says "WOW! The best answer is the question itself ;)". – Sandra Rossi Jan 16 '23 at 16:12

0 Answers0