Skip to Content
0

SAP Script for open automatically SAP GUI and run a transaction with input data from an access table

Nov 17, 2016 at 03:47 PM

441

avatar image

Hello everybody!

I finished here after googling and searching on youtube for a while with no luck...

I need to to do 2 things that I couldn't figure out how.. (sorry for the dummy question, I'm new in SAP scripting)

First of all, I need a way of opening SAP GUI and accessing to a particular server on the SAP list.

I could do the first with this code I found on the web:

set WshShell = CreateObject("WScript.Shell")

Set proc = WshShell.Exec("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe")

Do While proc.Status = 0

WScript.Sleep 100

Loop

That opens automatically SAP Logon but then I have a list of servers where I need to select only one to proceed to the credentials screen (from which I can start recording macros in SAP).

Is there a way of doing it?, since I cannot record macros in these steps... only from the credentials screen.

Second doubt: I recorded a simple code that loads user and password, enters a transaction and downloads some information to excel.

I put this code in an access where I can run it from a button in a form.

Now the user and password are fixed in the code, but I want to replace this part of the code with a variable (I guess that should be the way), which reads from a field in an access table (where I could load/change the user and password eventually).

The code is the following:

If Not IsObject(application) Then

Set SapGuiAuto = GetObject("SAPGUI")

Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

Set connection = application.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 application, "on"

End If

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

session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "USER"

session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "PASSWORD"

session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus

session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 9

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

If Not IsObject(application) Then

Set SapGuiAuto = GetObject("SAPGUI")

Set application = SapGuiAuto.GetScriptingEngine

End If

If Not IsObject(connection) Then

Set connection = application.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 application, "on"

End If

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

session.findById("wnd[0]/tbar[0]/okcd").text = "transaction"

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

session.findById("wnd[0]").sendVKey 82

session.findById("wnd[0]/usr/ctxtGD-TAB").text = "/TABLE_1"

session.findById("wnd[0]/usr/txtGD-MAX_LINES").text = "99999999"

session.findById("wnd[0]/usr/txtGD-MAX_LINES").setFocus

session.findById("wnd[0]/usr/txtGD-MAX_LINES").caretPosition = 8

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

session.findById("wnd[1]").sendVKey 0

session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/ctxtGS_SELFIELDS-LOW[2,1]").text = "society_1"

session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/ctxtGS_SELFIELDS-LOW[2,1]").setFocus

session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/ctxtGS_SELFIELDS-LOW[2,1]").caretPosition = 4

session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC").verticalScrollbar.position = 12

session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/ctxtGS_SELFIELDS-LOW[2,3]").text = "01.07.2016"

session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/ctxtGS_SELFIELDS-HIGH[3,3]").text = "30.09.2016"

session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/ctxtGS_SELFIELDS-HIGH[3,3]").setFocus

session.findById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/ctxtGS_SELFIELDS-HIGH[3,3]").caretPosition = 10

session.findById("wnd[0]/tbar[1]/btn[8]").press

session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").setCurrentCell -1,""

session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").selectAll

session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").contextMenu

session.findById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").selectContextMenuItem "&XXL"

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

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

session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").select

session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").setFocus

session.findById("wnd[1]").sendVKey 0

session.findById("wnd[1]").sendVKey 0

What should I do to replace USER and PASSWORD by a field in an table stored in an access?. The same for changing the dates for instance, since they should be modified everytime I run the report.

Sorry for the long question. I hope anybody can help me with this issue.

Thanks!

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Rüdiger Specht Nov 20, 2016 at 11:59 AM
0

Have a look here to the answer from Stefan:

https://archive.sap.com/discussions/thread/3340373

Share
10 |10000 characters needed characters left characters exceeded