Hi,
wonder if anyone of you will be able to help me...
I have created a macro in excel to register mass uploads in sap. the macro is working very well until an unexpected window in sap appears, for instance warning box that future action exist.
each time when any window not included in my recording appears macro stops their work and I need to proceed with current user manually.
what I want to do is create kind of loop in my vba code, thanks to which macro will not stop when unexpected window appear but will cancel proceeding for this user, in excel file will return value "not ok" and then will continue with next user on the list.
not sure if I was clear enough in my description
below is my try, unfortunately it doesn`t work right
thanks in advance for help!
Sub termination()
Dim personnelid, endofcontract As String
Dim i As Integer
i = 2
start:
If Not IsObject(lrw) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set lrw = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = lrw.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 lrw, "on"
End If
On Error GoTo handler
Do
Cells(i, 1).Formula = Trim(Replace(Cells(i, 1).Text, Chr(160), Chr(32)))
Cells(i, 2).Formula = Trim(Replace(Cells(i, 2).Text, Chr(160), Chr(32)))
Sheets("Termination").Select
personnelid = Cells(i, 1)
endofcontract = Cells(i, 2)
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/subSUBSCR_PERNR:SAPMP50A:0110/ctxtRP50G-PERNR").Text = personnelid
session.findById("wnd[0]/usr/ctxtRP50G-EINDA").Text = endofcontract
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/ctxtP0000-MASSG").Text = "12"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/usr/ctxtP0033-AUS04").Text = "0005"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/shellcont/shell/shellcont[0]/shell/shellcont[1]/shell[1]").hierarchyHeaderWidth = 295
Sheets("Termination").Select
Cells(i, 3).Select
ActiveCell.FormulaR1C1 = "OK"
i = i + 1
Loop While Cells(i, 1) <> ""
Exit Sub
handler:
session.findById("wnd[1]").Close
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
session.findById("wnd[0]/shellcont/shell/shellcont[0]/shell/shellcont[1]/shell[1]").hierarchyHeaderWidth = 310
session.findById("wnd[0]/usr/subSUBSCR_PERNR:SAPMP50A:0110/ctxtRP50G-PERNR").Text = ""
session.findById("wnd[0]/usr/ctxtRP50G-EINDA").Text = ""
session.findById("wnd[0]").sendVKey 0
Sheets("Termination").Select
Cells(i, 3).Select
ActiveCell.FormulaR1C1 = "NOK"
i = i + 1
GoTo start
End Sub