cancel
Showing results for 
Search instead for 
Did you mean: 

SAP 7.60 Paste value from SAP to excel via VBA

hbekmez
Discoverer
0 Kudos

I am successfully using Excel macro to create a new employee for our payroll system. It's not a problem to fill data from Excel to SAP. But I am struggling to write a single value back from SAP to Excel.

The following macro creates the employee in SAP and in that process a new id is created for that employee. What I do now is simply copy the new id from "RP50G-PERNR" and paste it to excel and proceed with the next macro, which uses that number. I am now looking for a way to expand my macro below to paste that single valie to a field in my Excel shield Sheets("HR").Range("B4").Value. And so far I have had no success. I would be grateful for some direction and help.

Sub SAP_Anlage()
If Not IsObject(App) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set App = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = App.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 App, "on"
End If
Application.DisplayAlerts = False
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/NPA40"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRP50G-PERNR").Text = ""
session.findById("wnd[0]/usr/ctxtRP50G-EINDA").Text = Sheets("HR").Range("C2").Value
session.findById("wnd[0]/usr/tblSAPMP50ATC_MENU_EVENT").getAbsoluteRow(0).Selected = True
session.findById("wnd[0]/usr/tblSAPMP50ATC_MENU_EVENT/txtT529T-MNTXT[0,0]").SetFocus
session.findById("wnd[0]/usr/tblSAPMP50ATC_MENU_EVENT/txtT529T-MNTXT[0,0]").caretPosition = 0
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/ctxtP0000-MASSG").Text = Sheets("HR").Range("E3").Value
session.findById("wnd[0]/usr/ctxtPSPAR-PLANS").Text = Sheets("HR").Range("F7").Value
session.findById("wnd[0]/usr/ctxtPSPAR-WERKS").Text = Sheets("HR").Range("B8").Value
session.findById("wnd[0]/usr/ctxtPSPAR-PERSG").Text = Sheets("HR").Range("A12").Value
session.findById("wnd[0]/usr/ctxtPSPAR-PERSK").Text = Sheets("HR").Range("B12").Value
session.findById("wnd[0]/usr/ctxtPSPAR-PERSK").SetFocus
session.findById("wnd[0]/usr/ctxtPSPAR-PERSK").caretPosition = 2
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0 'Erneut Entern falls nach Testabrechnung
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]/usr/cmbQ0002-ANREX").Key = Sheets("Persönliche Angaben").Range("BA58").Value
session.findById("wnd[0]/usr/txtP0002-NACHN").Text = Sheets("Persönliche Angaben").Range("D13").Value
session.findById("wnd[0]/usr/txtP0002-VORNA").Text = Sheets("Persönliche Angaben").Range("AB13").Value
session.findById("wnd[0]/usr/txtP0002-NAME2").Text = Sheets("Persönliche Angaben").Range("AX23").Value
session.findById("wnd[0]/usr/ctxtQ0002-GBPAS").Text = Sheets("Persönliche Angaben").Range("AB22").Value
session.findById("wnd[0]/usr/cmbP0002-GESCH").Key = Sheets("Persönliche Angaben").Range("AX58").Value
session.findById("wnd[0]/usr/txtP0002-GBORT").Text = Sheets("Persönliche Angaben").Range("D49").Value
session.findById("wnd[0]/usr/cmbP0002-GBLND").Key = Sheets("Persönliche Angaben").Range("BB49").Value
session.findById("wnd[0]/usr/cmbP0002-NATIO").Key = Sheets("Persönliche Angaben").Range("BB58").Value
session.findById("wnd[0]/usr/cmbP0002-NATIO").SetFocus
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/ctxtP0001-BTRTL").Text = Sheets("HR").Range("C8").Value
session.findById("wnd[0]/usr/ctxtP0001-GSBER").Text = Sheets("HR").Range("D7").Value
session.findById("wnd[0]/usr/ctxtP0001-SACHP").Text = Sheets("HR").Range("E11").Value
session.findById("wnd[0]/usr/ctxtP0001-SACHZ").Text = Sheets("HR").Range("F11").Value
session.findById("wnd[0]/usr/ctxtP0001-SACHA").Text = Sheets("HR").Range("G11").Value
session.findById("wnd[0]/usr/txtP0001-MSTBR").Text = Sheets("HR").Range("C11").Value
session.findById("wnd[0]/usr/txtP0001-MSTBR").SetFocus
session.findById("wnd[0]/usr/txtP0001-MSTBR").caretPosition = 3
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 0
Application.DisplayAlerts = True
End Sub

Accepted Solutions (0)

Answers (1)

Answers (1)

hbekmez
Discoverer
0 Kudos

Thanks, I figured it out myself and it was embarassingly easy.