Hi experts,
I need help. I make the script for report to SAP with Excel.
In the MM03, has the position of stock of my deposits. But if the material was no created position for him, we have a error. How to make ?
The code:
Thanks
Sub run()
If Not IsObject(AppSapGui) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set AppSapGui = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = AppSapGui.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 AppSapGui, "on"
End If
session.findById("wnd[0]").maximize
For x = 5 To 5000
If Cells(x, 1) = "" Then Exit For
session.findById("wnd[0]/tbar[0]/okcd").Text = "/NMM03"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").Text = Cells(x, 1)
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ctxtRMMG1-WERKS").Text = "7351"
session.findById("wnd[1]/usr/ctxtRMMG1-LGORT").Text = Cells(x, 4) 'dep
session.findById("wnd[1]/usr/ctxtRMMG1-LGORT").SetFocus
session.findById("wnd[1]/usr/ctxtRMMG1-LGORT").caretPosition = 4
session.findById("wnd[1]/tbar[0]/btn[0]").press
Cells(x, 2) = session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP20/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1005/txtMAKT-MAKTX").Text
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP20/ssubTABFRA1:SAPLMGMM:2000/subSUB1:SAPLMGD1:1005/txtMAKT-MAKTX").caretPosition = 10
Cells(x, 5) = session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP20/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2701/txtMARD-LGPBE").Text 'postiotn
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP20/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2701/txtMARD-LGPBE").caretPosition = 3
session.findById("wnd[0]").sendVKey 0
Cells(x, 3) = session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP30/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:3002/txtMARD-LABST").Text
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP30/ssubTABFRA1:SAPLMGMM:2000/subSUB3:SAPLMGD1:3002/txtMARD-LABST").caretPosition = 12
session.findById("wnd[0]/tbar[0]/btn[3]").press
Next
End Sub