Skip to Content
0
Jul 03, 2019 at 08:14 AM

SAP GUI Scripting (Loop , reference variable) No background in VBA,pls help

1497 Views Last edit Jul 03, 2019 at 03:20 AM 2 rev

Hi All, I have searched multiple threads here but because I have no background in terms of VBA, I have difficulties understanding them. I really appreciate if anybody could help. This piece of code should help me copy paste a range of cells from Excel to SAP. Problems encountered:

1) I am trying to set the session.findbyid part ("[0,0]") or ("[0,3]") etc. this bolded 0 (which I assume is the row position in SAP, to move to the next row ) i.e instead of [0,0] [1,0] [2,0], I am trying to create a loop. I have tried this method https://answers.sap.com/questions/5701329/referencing-a-variable-in-session-findbyid-command.html by using this "Cstr" but doesnt work.

2) I am also trying to loop this objSheet.Cells(10, 1) rows

some of the codes I found here in the threads are as below but it doesnt work as well

Dim objSheet, intRow, Row

For Row = 10

Row = Row + 1

Next Row

This is my current code with the SAP connections code and the code reference to Excel that I have copied from the threads here.

Dim Session

Set SapGuiAuto = GetObject("SAPGUI")

Set SetApp = SapGuiAuto.GetScriptingEngine

Set Connection = SetApp.Children(0)

Set Session = Connection.Children(0)

Session.findById("wnd[0]").maximize

Set objSheet = ActiveWorkbook.Sheets(2)

Session.findById("wnd[0]/tbar[0]/okcd").Text = "/nf-65"

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

Session.findById("wnd[0]/usr/ctxtBKPF-BLDAT").Text = objSheet.Cells(3, 2)

Session.findById("wnd[0]/usr/ctxtBKPF-BUDAT").Text = objSheet.Cells(3, 2)

Session.findById("wnd[0]/usr/ctxtBKPF-BUKRS").Text = objSheet.Cells(4, 2)

Session.findById("wnd[0]/usr/txtBKPF-MONAT").Text = objSheet.Cells(5, 2)

Session.findById("wnd[0]/usr/ctxtBKPF-WAERS").Text = objSheet.Cells(6, 2)

Session.findById("wnd[0]/usr/ctxtBKPF-WAERS").SetFocus

Session.findById("wnd[0]/usr/ctxtBKPF-WAERS").caretPosition = 3

Session.findById("wnd[0]/tbar[1]/btn[20]").press

Session.findById("wnd[0]/mbar/menu[4]/menu[0]").Select Session.findById("wnd[1]/usr/sub:SAPLF034:1100/txtT021G-LTEXT[3,11]").SetFocus Session.findById("wnd[1]/usr/sub:SAPLF034:1100/txtT021G-LTEXT[3,11]").caretPosition = 5 Session.findById("wnd[1]").sendVKey 2

Session.findById("wnd[0]/usr/sub:SAPLF040:8003/ctxtBSEG-BSCHL[0,0]").Text = objSheet.Cells(10, 1)

Session.findById("wnd[0]/usr/sub:SAPLF040:8003/ctxtRF05V-KONTO[0,3]").Text = objSheet.Cells(10, 2) Session.findById("wnd[0]/usr/sub:SAPLF040:8003/txtBSEG-WRBTR[0,14]").Text = objSheet.Cells(10, 3) Session.findById("wnd[0]/usr/sub:SAPLF040:8003/ctxtBSEG-KOSTL[0,31]").Text = objSheet.Cells(10, 4) Session.findById("wnd[0]/usr/sub:SAPLF040:8003/ctxtBSEG-SGTXT[0,42]").Text = objSheet.Cells(10, 5) Session.findById("wnd[0]/usr/sub:SAPLF040:8003/ctxtBSEG-BSCHL[1,0]").Text = objSheet.Cells(11, 1) Session.findById("wnd[0]/usr/sub:SAPLF040:8003/ctxtRF05V-KONTO[1,3]").Text = objSheet.Cells(11, 2) Session.findById("wnd[0]/usr/sub:SAPLF040:8003/txtBSEG-WRBTR[1,14]").Text = objSheet.Cells(11, 3) Session.findById("wnd[0]/usr/sub:SAPLF040:8003/ctxtBSEG-KOSTL[1,31]").Text = objSheet.Cells(11, 4) Session.findById("wnd[0]/usr/sub:SAPLF040:8003/ctxtBSEG-SGTXT[1,42]").Text = objSheet.Cells(11, 5)

Session.findById("wnd[0]/usr").verticalScrollbar.Position = 23

Session.findById("wnd[0]/usr/sub:SAPLF040:8003/ctxtBSEG-SGTXT[15,42]").SetFocus Session.findById("wnd[0]/usr/sub:SAPLF040:8003/ctxtBSEG-SGTXT[15,42]").caretPosition = 25 Session.findById("wnd[0]/tbar[1]/btn[14]").press

Session.findById("wnd[0]/usr/txtBKPF-BKTXT").Text = objSheet.Cells(7, 2)

Session.findById("wnd[0]/usr/txtBKPF-BKTXT").SetFocus

Session.findById("wnd[0]/usr/txtBKPF-BKTXT").caretPosition = 25

End Sub