Skip to Content
0
Mar 20, 2019 at 10:08 AM

SAP GUI script (recording) loop

1796 Views

Hi,

I tried to find something that would answer my question but I can't seem to find it. I did find how to start a loop, but after that I am going completely wrong. The loop keeps going until I get some error. At first I didn't define the MSG_TXT and only used the message type, but that wasn't working before either.

In short: I need in transaction CS02 to edit the lead time (offset) of all items. So I loop the values for this. This part actually does work, but I can't seem to stop the loop. The amount of items is variable so I can't put in a number on how often to do this loop either (if that would even be possible). Can anyone help me on how to properly exit? The only sofar I see that sort of let the system know that it is the last item is the text from the sbar which is apparently a 'succes' message that says "Last Item".

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


'Connect with excel, to a workbook located in "x" and use sheet named "z"
Set xclapp = CreateObject("Excel.Application")
Set xclwbk = xclapp.Workbooks.Open("D:\Users\david_lindeboom\AppData\Roaming\SAP\SAP GUI\Scripts\LEADTIME.xlsx")
set xclsht = xclwbk.Sheets("Days")


'Define your columns. i = row, j = column
for i = 2 to xclapp.ActiveCell.SpecialCells(11).Row 'i=2 because row 1 are headers
 for j = 1 to xclapp.ActiveCell.SpecialCells(11).Column
if j=1 then MATNR = xclsht.Cells(i,j).Value
if j=2 then WERKS = xclsht.Cells(i,j).Value
if j=3 then STLAN = xclsht.Cells(i,j).Value

Next

'starts the transaction in first SAP tab.
myTransaction = "CS02"
session.findById("wnd[0]/tbar[0]/okcd").text = "/n" & myTransaction
session.findById("wnd[0]").sendVKey 0
On error Resume Next

session.findById("wnd[0]/usr/ctxtRC29N-MATNR").text = MATNR
session.findById("wnd[0]/usr/ctxtRC29N-WERKS").text = WERKS
session.findById("wnd[0]/usr/ctxtRC29N-STLAN").text = STLAN
session.findById("wnd[0]/usr/ctxtRC29N-STLAN").setFocus
session.findById("wnd[0]/usr/ctxtRC29N-STLAN").caretPosition = 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[27]").press
session.findById("wnd[0]/tbar[1]/btn[7]").press


'loop this part
Do 

'Define Last Item
MSG_TXT = Session.FindById("wnd[0]/sbar").messagetype = "S"


session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPHPT/ssubSUBPAGE:SAPLCSDI:0830/txtRC29P-NLFZT").text = "23"
session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPHPT/ssubSUBPAGE:SAPLCSDI:0830/txtRC29P-NLFZV").text = "1"
session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPHPT/ssubSUBPAGE:SAPLCSDI:0830/ctxtRC29P-NLFMV").text = "DAY"
session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPHPT/ssubSUBPAGE:SAPLCSDI:0830/ctxtRC29P-NLFMV").setFocus
session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPHPT/ssubSUBPAGE:SAPLCSDI:0830/ctxtRC29P-NLFMV").caretPosition = 3
session.findById("wnd[0]/tbar[1]/btn[18]").press


On error resume next

If MSG_TXT = "S" Then exit Do

Loop

On Error Resume Next
'Message output for specific messagetype
'S = succes, W = warning, E = error, I = information, A = abort
'If Session.FindById("wnd[0]/sbar").messagetype = "I" Then xclsht.Cells(i, 8).Value = session.findById("wnd[0]/sbar").Text
'If Session.FindById("wnd[0]/sbar").messagetype = "E" Then xclsht.Cells(i, 9).Value = session.findById("wnd[0]/sbar").Text
'If Session.FindById("wnd[0]/sbar").messagetype = "W" Then xclsht.Cells(i, 10).Value = session.findById("wnd[0]/sbar").Text
'If Session.FindById("wnd[0]/sbar").messagetype = "A" Then xclsht.Cells(i, 11).Value = session.findById("wnd[0]/sbar").Text
If Session.FindById("wnd[0]/sbar").messagetype = "S" Then xclsht.Cells(i, 4).Value = session.findById("wnd[0]/sbar").Text

session.findById("wnd[0]/tbar[0]/btn[11]").press

On Error Resume Next

'might be some redundant, just to cover all possible messages
If Session.FindById("wnd[0]/sbar").messagetype = "I" Then xclsht.Cells(i, 5).Value = session.findById("wnd[0]/sbar").Text
If Session.FindById("wnd[0]/sbar").messagetype = "E" Then xclsht.Cells(i, 5).Value = session.findById("wnd[0]/sbar").Text
If Session.FindById("wnd[0]/sbar").messagetype = "W" Then xclsht.Cells(i, 5).Value = session.findById("wnd[0]/sbar").Text
If Session.FindById("wnd[0]/sbar").messagetype = "A" Then xclsht.Cells(i, 5).Value = session.findById("wnd[0]/sbar").Text
If Session.FindById("wnd[0]/sbar").messagetype = "S" Then xclsht.Cells(i, 5).Value = session.findById("wnd[0]/sbar").Text

'xclsht.Cells(i, 6).Value = session.findById("wnd[0]/sbar").Text

On Error GoTo 0

Next

msgbox "All " & cstr(xclapp.ActiveCell.SpecialCells(11).Row - 1) & " Excel rows have been processed."

xclapp.ScreenUpdating = true
xclapp.ActiveWorkbook.Save
Set xclwbk = Nothing
Set xclsht = Nothing
xclapp.Quit
Set xclapp = Nothing