ITPub博客

首页 > 数字化转型 > ERP > SAP GUI Scripting

SAP GUI Scripting

原创 ERP 作者:jylbgogo 时间:2018-06-21 14:59:43 0 删除 编辑

SAP GUI Scripting is written in Microsoft’s VBScript language. You can use Script to automate business transactions, to automate master data creation, to automate configuration, etc.

一、Set up your Environment for SAP GUI Scripting

1. Set up your client





 

2. Set up your SAP system




二、Ready to run scripts


1- Generate the Script (*.vbs)



The Script file can be generated automatically by recording the steps executed in SAP.

The following screenshots show how to create the main script file in a few minutes or even second:




It is important to select the file extension .vbs:


Once you press Start Recording, you will see this indicator at the bottom of the screen:


Execute the transaction that you want to save in the script. Depending on what you are trying to automate, you will need to enter the SAP t-code with /n. If you do this you will leave and you will access the t-code again in each iteration.




Enter all the fields that you are going to automate:

[ Here just do the operation of MD11]


Once one iteration is completed, press Stop Recording. This will complete the generation of the Script and the file will be saved in the selected Path.



2- Enhance the Script (*.vbs) to allow the automation via Excel


You can open the Script by TXT.




To allow the automation, it is necessary to do two things:

    Add two blocks of programming code in the vbs: There is no need to understand how to program a script. The code is always the same. The only thing that could change is the number of columns used in the excel. In this example we are going to use 5 columns.




This is the code of each block of code. If you need to add columns in the excel, you will need to add the sentence in red color:

Block of code: Block A

REM ADDED BY EXCEL *************************************

Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(,”Excel.Application”)
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

For i = 2 to objSheet.UsedRange.Rows.Count
COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) ‘Column1
COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) ‘Column2
COL3 = Trim(CStr(objSheet.Cells(i, 3).Value)) ‘Column3
COL4 = Trim(CStr(objSheet.Cells(i, 4).Value)) ‘Column4
COL5 = Trim(CStr(objSheet.Cells(i, 5).Value)) ‘Column5

COL6 = Trim(CStr(objSheet.Cells(i, 6).Value)) ‘Column6

REM ADDED BY EXCEL *************************************

Block of code: Block B

REM FINALIZATION CONTROL CHECK ************************

aux=col1 & ” ” & col2 & ” ” & col3  & ” ” & col4 & ” ” & col5  & ” ” & col6
CreateObject(“WScript.Shell”).run(“cmd /c @echo %date% %time% ” & aux & ” >> C:\SCRIPT\PlOrCreationLog.txt”)
next
msgbox “Process Completed”

REM FINALIZATION CONTROL CHECK ************************

Replace the “hardcoaded code” by the Columns#:






This is the final code of the Script (in red color the text added):

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

REM ADDED BY EXCEL *************************************

Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(,”Excel.Application”)
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet

For i = 2 to objSheet.UsedRange.Rows.Count
COL1 = Trim(CStr(objSheet.Cells(i, 1).Value)) ‘Column1
COL2 = Trim(CStr(objSheet.Cells(i, 2).Value)) ‘Column2
COL3 = Trim(CStr(objSheet.Cells(i, 3).Value)) ‘Column3
COL4 = Trim(CStr(objSheet.Cells(i, 4).Value)) ‘Column4
COL5 = Trim(CStr(objSheet.Cells(i, 5).Value)) ‘Column5

REM ADDED BY EXCEL *************************************

session.findById(“wnd[0]/tbar[0]/okcd”).text = “/nmd11”
session.findById(“wnd[0]”).sendVKey 0
session.findById(“wnd[0]/usr/ctxtRM61P-PASCH”).text = COL1
session.findById(“wnd[0]/usr/ctxtRM61P-PASCH”).caretPosition = 2
session.findById(“wnd[0]”).sendVKey 0
session.findById(“wnd[0]/usr/ctxtPLAF-MATNR”).text = COL2
session.findById(“wnd[0]/usr/ctxtPLAF-PLWRK”).text = COL3
session.findById(“wnd[0]/usr/tabsTABTC/tabpTAB01/ssubINCLUDE1XX:SAPLM61O:0711/subINCLUDE711_1:SAPLM61O:0802/txtPLAF-GSMNG”).text = COL4
session.findById(“wnd[0]/usr/tabsTABTC/tabpTAB01/ssubINCLUDE1XX:SAPLM61O:0711/subINCLUDE711_1:SAPLM61O:0802/ctxtPLAF-PSTTR”).text = COL5
session.findById(“wnd[0]/usr/tabsTABTC/tabpTAB01/ssubINCLUDE1XX:SAPLM61O:0711/subINCLUDE711_1:SAPLM61O:0802/ctxtPLAF-PSTTR”).setFocus
session.findById(“wnd[0]/usr/tabsTABTC/tabpTAB01/ssubINCLUDE1XX:SAPLM61O:0711/subINCLUDE711_1:SAPLM61O:0802/ctxtPLAF-PSTTR”).caretPosition = 10
session.findById(“wnd[0]”).sendVKey 0
session.findById(“wnd[0]/tbar[0]/btn[11]”).press

REM FINALIZATION CONTROL CHECK ************************

aux=col1 & ” ” & col2 & ” ” & col3  & ” ” & col4 & ” ” & col5
CreateObject(“WScript.Shell”).run(“cmd /c @echo %date% %time% ” & aux & ” >> C:\SCRIPT\PlOrCreationLog.txt”)
next
msgbox “Process Completed”

REM FINALIZATION CONTROL CHECK ************************


3- Maintain the Excel file

Enter all the data to be used in the automation. The script will always start reading from the second line. Use the first line for the header:

Important: Only one excel file should be opened to execute the Script



4- Script Execution


Remember the previous message: Only one excel file should be opened to execute the Script


Keep your Excel file opened. Open the Script Recording and Playback toolkit, Select the path and the file and press Play.


You will see how it works!!


Notice: If you copy code here, you need to pay attention to the format of punctuation. Or you will get a error if the punctuation not in English.

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31534380/viewspace-2156448/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2018-04-27

  • 博文量
    8
  • 访问量
    3872