How to pass dynamic variable in SAP GUI scripting code

Hi Team,

Need to create PO order in SAP.
The data coming from excel row by row need to create PO using SAP GUI scripting.
Had recorded the code also stuck at passing dynamic variable it is not working as expected.
Help would be highly appreciated as soon as possible.

Thanks & regards
Suraj

You can achieve this using SAP GUI scripting with VBScript and Excel automation. Here’s a basic structure that reads each row from Excel and creates a PO in SAP dynamically:

Set objExcel = CreateObject(“Excel.Application”)
objExcel.Visible = False
Set objWorkbook = objExcel.Workbooks.Open(“C:\YourPath\PO_Data.xlsx”)
Set objSheet = objWorkbook.Sheets(1)

row = 2
Do While objSheet.Cells(row, 1).Value <> “”

mat     = objSheet.Cells(row, 1).Value
qty     = objSheet.Cells(row, 2).Value
vendor  = objSheet.Cells(row, 3).Value

Connect to SAP
Set SapGuiAuto  = GetObject("SAPGUI")
Set SAPApp      = SapGuiAuto.GetScriptingEngine
Set SAPCon      = SAPApp.Children(0)
Set session     = SAPCon.Children(0)

session.StartTransaction "ME21N"

WScript.Sleep 2000 

Header data
session.findById("wnd[0]/usr/ctxtRM06E-BSART").Text = "NB"
session.findById("wnd[0]/usr/ctxtEKKO-LIFNR").Text = vendor

Item data
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0014/subSUB1:SAPLMEGUI:1201/tblSAPLMEGUITC_1201/ctxtMEPO1211-MATNR[1,0]").Text = mat
session.findById("wnd[0]/usr/subSUB0:SAPLMEGUI:0014/subSUB1:SAPLMEGUI:1201/tblSAPLMEGUITC_1201/txtMEPO1211-MENGE[3,0]").Text = qty

Save the PO
session.findById("wnd[0]/tbar[0]/btn[11]").Press

row = row + 1

Loop

objWorkbook.Close False
objExcel.Quit
Set objExcel = Nothing

If this works please mark it as SOLUTION

Happy Automation

Hello @suraj_singh3

To achieve this you can:

  • Use a Read Range activity to read the data from the Excel file and save as a Datatable
  • Use a For Each Row in Data Table activity to loop the rows
  • Use an Assign activity (or Multiple Assign) to grab the needed values for the dynamic selector.
  • Use UI Activities, such as Type Into, Click etc. to fill out the PO in SAP
  • Edit descriptors/selectors on the activities where you replace fixed values with your variables by right clicking and chosing “Use variable”.

Regards
Soren

@suraj_singh3,

I assume your script is in text/string format stored somewhere. If yes, just place a placeholder in that string and just before try to run that script, replace that placeholder with the dynamic variable.

For example

Your script should be like this
session.findById("wnd[0]/usr/ctxtXYZ").Text = "#Placeholder#"

Now use assign activity to replace the placeholder with variable like this

strYourScript = strYourScript.Replace("#Placeholder#",strYourDynamicVariable)

@suraj_singh3

Mostly you might be using invoke vbs if so then use input arguments

Wscript.Arguments(0) is used for first like that increase number for each

Cheers

Cheers