I've got a problem with a VBA SAP script. I have data in excel and I want to execute the transaction code IW41
by copying and pasting data from Excel. I have all of the data like dates, number of orders, who did it etc in Excel and I want to automate it. I did a loop for variable i
but I get an error and I cannot fix it.
Error :
Run-time error '619': Application-defined or object-defined error
Code provided below.
Can you give me some pro tips or help me fix it?
Screen in IW41
where the error occurs:
Code :
Sub ConfirmPM_Nots()
SystemName = "CCP" 'change as needed or use a variable
Transaction = "SESSION_MANAGER" 'change as needed or use a variable
On Error GoTo ErrorHandler:
If Not IsObject(Sap_Applic) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set Sap_Applic = SapGuiAuto.GetScriptingEngine
End If
On Error GoTo 0
koniec:
qConnections = Sap_Applic.Connections.Count
If qConnections = 0 Then
MsgBox "No connection to SAP"
End
End If
bSession = False
For iConnectionCounter = 0 To qConnections - 1
Set Connection = Sap_Applic.Children(Int(iConnectionCounter))
If Not Connection.Description = "" Then
qSessions = Connection.Children.Count
For iSessionCounter = 0 To qSessions - 1
Set session = Connection.Children(Int(iSessionCounter))
If session.info.SystemName <> SystemName Then Exit For
If session.info.Transaction = Transaction Then
bSession = True
Exit For
End If
Next
End If
If bSession Then Exit For
Next
If Not bSession Then
MsgBox SystemName & " not available or free session not available"
End
End If
Do
i = 1
session.findById("wnd[0]").resizeWorkingPane 128, 37, False
session.findById("wnd[0]/tbar[0]/okcd").Text = "iw41"
session.findById("wnd[0]").sendVKey 0
Order = Cells(i, 1)
b = Cells(i, 2)
c = Cells(i, 3)
d = Cells(i, 4)
session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").Text = Order
session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").caretPosition = 7
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").SetFocus
session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").caretPosition = 2
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/chkAFRUD-AUERU").Selected = True
session.findById("wnd[0]/usr/chkAFRUD-LEKNW").Selected = True
session.findById("wnd[0]/usr/ctxtAFRUD-ISDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-IDAUR").Text = b
session.findById("wnd[0]/usr/ctxtAFRUD-IEDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").Text = d
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").SetFocus
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").caretPosition = 10
session.findById("wnd[0]/tbar[0]/btn[11]").press
i = i + 1
Loop
Exit Sub
ErrorHandler:
MsgBox "No connection to SAP"
End
End Sub
"iw41"
(from session.findById("wnd[0]/tbar[0]/okcd").Text = "iw41"
) only works if the current screen is the start menu. OK-Code "/niw41"
will always work! So this code might work (untested):
Do
i = 1
' session.findById("wnd[0]").resizeWorkingPane 128, 37, False
' iw41 only works in the start menu. OK-Code /niw41 will always work!
session.findById("wnd[0]/tbar[0]/okcd").Text = "/niw41"
session.findById("wnd[0]").sendVKey 0
Order = Cells(i, 1)
b = Cells(i, 2).value
c = Cells(i, 3).value
d = Cells(i, 4).value
session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").Text = Order
' session.findById("wnd[0]/usr/ctxtCORUF-AUFNR").caretPosition = 7
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").SetFocus
' session.findById("wnd[0]/usr/tblSAPLCORUTC_3100/txtAFVGD-VORNR[1,0]").caretPosition = 2
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/chkAFRUD-AUERU").Selected = True
session.findById("wnd[0]/usr/chkAFRUD-LEKNW").Selected = True
session.findById("wnd[0]/usr/ctxtAFRUD-ISDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-IDAUR").Text = b
session.findById("wnd[0]/usr/ctxtAFRUD-IEDD").Text = c
session.findById("wnd[0]/usr/txtAFRUD-LTXA1").Text = d
' session.findById("wnd[0]/usr/txtAFRUD-LTXA1").SetFocus
' session.findById("wnd[0]/usr/txtAFRUD-LTXA1").caretPosition = 10
session.findById("wnd[0]/tbar[0]/btn[11]").press
i = i + 1
Loop
I also commented the lines with resizeWorkingPane
, caretPosition
and SetFocus
because this is usually not needed. resizeWorkingPane
will resize the SAPGUI screen and caretPosition
is the position of a cursor within a textbox. Sometimes needed if you want to replace text for example. But in this case certainly not.