-2

Below is SAP script for deleting specific rows in Source List. Upon entering material number and plant there is possiblity of Warning messagees (Pending Obsolence, Requires exemption)- To continue to the next step an Enter session.findById("wnd[0]").sendVKey 0 is required once, twice or even 3 times. Again it depands on number of Warning messages on material.

In below example I have Enter 3 times but instead of using Enter 3 times by defalut I would like it to be more dynamic. If or as long there is an Warning message it should countionue to use Enter else move to next step in the script.

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
    If IsEmpty(objSheet.Cells(i, 4)) Then
COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1
COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2
COL3 = Trim(CStr(objSheet.Cells(i, 3).Value)) 'Column3


session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "/nme01"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtEORD-MATNR").text = COL1 'Material number
session.findById("wnd[0]/usr/ctxtEORD-WERKS").text = COL2 'Plant


session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0


session.findById("wnd[0]/usr/tblSAPLMEORTC_0205").getAbsoluteRow(COL3-1).selected = true 'Row to delete in SL
session.findById("wnd[0]/usr/tblSAPLMEORTC_0205/ctxtEORD-VDATU[0,6]").setFocus
session.findById("wnd[0]/usr/tblSAPLMEORTC_0205/ctxtEORD-VDATU[0,6]").caretPosition = 0
session.findById("wnd[0]/tbar[1]/btn[14]").press
session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
session.findById("wnd[0]/tbar[0]/btn[11]").press


objSheet.Cells(i, 4) = "Deleted"


aux=COL1 & " " & COL2 & " " & COL3
CreateObject("WScript.Shell").run("cmd /c @echo %date% %time% " & aux & " >> C:\SCRIPT\PlOrCreationLog.txt")

End If
next

msgbox "Process Completed"
Storax
  • 11,158
  • 3
  • 16
  • 33
Adde
  • 45
  • 2
  • 10
  • You've got to check the SAP status bar for a warning message and as long as there is one you press enter. – Storax Dec 27 '20 at 20:04
  • I tried something like this but how do I make it press enter as long as there is Warning message? Below only works if there is only one message. `if session.findById("wnd[0]/sbar").messageType = "W" then session.findById("wnd[0]").sendVKey 0 end if` – Adde Dec 27 '20 at 20:14
  • This should help you https://www.tutorialspoint.com/vbscript/vbscript_do_while_loop.htm – Storax Dec 27 '20 at 20:29
  • Did check that link before. Checked other examples of Do While loop too. Been trying for several days. Hard to beleive it I know. I can't beleive it my self when I see how simple it is... – Adde Dec 27 '20 at 21:08

1 Answers1

0

Try this code

Do While session.findById("wnd[0]/sbar").messageType = "W"        
    session.findById("wnd[0]").sendVKey 0 
Loop

The complete code should possibly look like this

    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
        If IsEmpty(objSheet.Cells(i, 4)) Then
    COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) 'Column1
    COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) 'Column2
    COL3 = Trim(CStr(objSheet.Cells(i, 3).Value)) 'Column3
    
    
    session.findById("wnd[0]").maximize
    session.findById("wnd[0]/tbar[0]/okcd").text = "/nme01"
    session.findById("wnd[0]").sendVKey 0
    session.findById("wnd[0]/usr/ctxtEORD-MATNR").text = COL1 'Material number
    session.findById("wnd[0]/usr/ctxtEORD-WERKS").text = COL2 'Plant
    
    
    session.findById("wnd[0]").sendVKey 0    ' <= You need to press Enter at least once before you check for a warning in the statu bar
    Do While session.findById("wnd[0]/sbar").messageType = "W"        
       session.findById("wnd[0]").sendVKey 0 
    Loop
    
    
    session.findById("wnd[0]/usr/tblSAPLMEORTC_0205").getAbsoluteRow(COL3-1).selected = true 'Row to delete in SL
    session.findById("wnd[0]/usr/tblSAPLMEORTC_0205/ctxtEORD-VDATU[0,6]").setFocus
    session.findById("wnd[0]/usr/tblSAPLMEORTC_0205/ctxtEORD-VDATU[0,6]").caretPosition = 0
    session.findById("wnd[0]/tbar[1]/btn[14]").press
    session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
    session.findById("wnd[0]/tbar[0]/btn[11]").press
    
    
    objSheet.Cells(i, 4) = "Deleted"
    
    
    aux=COL1 & " " & COL2 & " " & COL3
    CreateObject("WScript.Shell").run("cmd /c @echo %date% %time% " & aux & " >> C:\SCRIPT\PlOrCreationLog.txt")
    
    End If
    next
    
    msgbox "Process Completed"
Storax
  • 11,158
  • 3
  • 16
  • 33