Tip: SAP GUI Scripting Development Scenarios in the Context of UiPath

To automate user activities on the SAP GUI for Windows you can use SAP GUI Scripting. The API is in a normal case available with the installation of the SAP GUI for Windows. You can use this API with any language which supports COM. If the SAP GUI Scripting is installed and enabled on the backend you can use it for your automation cases.

To record your activities on the frontend you can use the standard SAP scripting recorder. Also you can use Scripting Tracker. It is a utility and a replacement to the SAP GUI Scripting Development Tools. It is an SAP GUI analyser, recorder, comparator on SAP GUI Scripting base and a SAP GUI Scripting API viewer. This flexibel tool can also be used in the context of UiPath. In this post I show four integration scenarios with different scripting languages.

Each script in the different language is recorded with Scripting Tracker and does the same. It opens transaction code SE16 and reads the count of database entries of the table TADIR. This is a trivial example, but shows us the possibilities on a simple example. For the communication with the script I use different methods e.g. like file, clipboard or return value.

VBScript

VBScript is an old fashioned scripting language and it is recommended to use it only for existing scripts and not for new projects. There are many sources available, here is an example of using an existing VBScript in the context of UiPath.
'-Begin-----------------------------------------------------------------

Option Explicit

Dim ConnectionNumber, SessionNumber

ConnectionNumber = WScript.Arguments.Item(0)
SessionNumber = WScript.Arguments.Item(1)

Dim SapGuiAuto, application, connection, session, dbCount

If Not IsObject(application) Then
  Set SapGuiAuto = GetObject("SAPGUI")
  Set application = SapGuiAuto.GetScriptingEngine
End If

If Not IsObject(connection) Then
  Set connection = application.Children(CInt(ConnectionNumber))
End If

If Not IsObject(session) Then
  Set session = connection.Children(CInt(SessionNumber))
End If

session.findById("wnd[0]/tbar[0]/okcd").text = "/nSE16"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").text = "TADIR"
session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").caretPosition = 5
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").sendVKey 31

dbCount = session.findById("wnd[1]/usr/txtG_DBCOUNT").Text

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

WScript.Echo CStr(dbCount)

'-End-------------------------------------------------------------------

Hint: If you use Option Explicit you must define the arguments of Invoke VBScript activity too.

Variables in UiPath

Sequence in UiPath

Hint: Store the VBScript file into your project folder.
003

Properties of Invoke VBScript activity
004

Arguments of Invoke VBScript activity

PowerShell

PowerShell is the current scripting language of Windows.
#-Begin-----------------------------------------------------------------

#-Parameters------------------------------------------------------------
Param(
  [String]$ConnectionNumber = "0",
  [String]$SessionNumber = "0"
)

#-Includes--------------------------------------------------------------
."$PSScriptRoot\COM.ps1";

#-Main------------------------------------------------------------------
$SapGuiAuto = Get-Object( , "SAPGUI");
If ($SapGuiAuto -isnot [__ComObject]) {
  Exit;
}

$application = Invoke-Method $SapGuiAuto "GetScriptingEngine";
If ($application -isnot [__ComObject]) {
  Free-Object $SapGuiAuto;
  Exit;
}

$connection = Get-Property $application "Children"@([convert]::ToInt32($ConnectionNumber, 10));
If ($Null -eq $connection) {
  Free-Object $SapGuiAuto;
  Exit;
}

$session = Get-Property $connection "Children"@([convert]::ToInt32($SessionNumber, 10));
If ($Null -eq $session) {
  Free-Object $SapGuiAuto;
  Exit;
}
$ID = Invoke-Method $session "findById" @("wnd[0]/tbar[0]/okcd");
Set-Property $ID "text" @("/nSE16");
$ID = Invoke-Method $session "findById" @("wnd[0]");
Invoke-Method $ID "sendVKey" @(0);
$ID = Invoke-Method $session "findById" @("wnd[0]/usr/ctxtDATABROWSE-TABLENAME");
Set-Property $ID "text" @("TADIR");
$ID = Invoke-Method $session "findById" @("wnd[0]/usr/ctxtDATABROWSE-TABLENAME");
Set-Property $ID "caretPosition" @(5);
$ID = Invoke-Method $session "findById" @("wnd[0]");
Invoke-Method $ID "sendVKey" @(0);
$ID = Invoke-Method $session "findById" @("wnd[0]");
Invoke-Method $ID "sendVKey" @(31);

$ID = Invoke-Method $session "findById" @("wnd[1]/usr/txtG_DBCOUNT");
$dbCount = Get-Property $ID "text";

$ID = Invoke-Method $session "findById" @("wnd[1]/tbar[0]/btn[0]");
Invoke-Method $ID "press";
$ID = Invoke-Method $session "findById" @("wnd[0]");
Invoke-Method $ID "sendVKey" @(3);
$ID = Invoke-Method $session "findById" @("wnd[0]");
Invoke-Method $ID "sendVKey" @(3);

Free-Object $SapGuiAuto;

Set-Content dbCount.txt $dbCount;

#-End-------------------------------------------------------------------

Variables in UiPath

Sequence in UiPath

Hint: Store the PowerShell script file and the include into your project folder.
008

Properties of Start-Process activity

Code for Invoke Code activity

'-Begin-----------------------------------------------------------------

Dim p() As Process

Do
  p = System.Diagnostics.Process.GetProcessesByName("powershell")
  System.Threading.Thread.Sleep(500)
Loop Until p.Length = 0

'-End-------------------------------------------------------------------

Hint: To get the result from the PowerShell script the content of the file dbCount.txt is read.

AutoIt

;-Begin-----------------------------------------------------------------

AutoItSetOption("MustDeclareVars", 1)

Dim $ConnectionNumber, $SessionNumber
Dim $SapGuiAuto, $application, $connection, $session, $dbCount

$ConnectionNumber = Number($CmdLine[1])
$SessionNumber = Number($CmdLine[2])

$SapGuiAuto = ObjGet("SAPGUI")
If Not IsObj($SapGuiAuto) Or @Error Then
  Exit
EndIf

$application = $SapGuiAuto.GetScriptingEngine()
If Not IsObj($application) Then
  Exit
EndIf

$connection = $application.Children($ConnectionNumber)
If Not IsObj($connection) Then
  Exit
EndIf

$session = $connection.Children($SessionNumber)
If Not IsObj($session) Then
  Exit
EndIf

$session.findById("wnd[0]/tbar[0]/okcd").text = "/nSE16"
$session.findById("wnd[0]").sendVKey(0)
$session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").text = "TADIR"
$session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").caretPosition = 5
$session.findById("wnd[0]").sendVKey(0)
$session.findById("wnd[0]").sendVKey(31)

$dbCount = $session.findById("wnd[1]/usr/txtG_DBCOUNT").text

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

Clipput($dbCount)

;-End-------------------------------------------------------------------

Variables in UiPath

Sequence in UiPath

Hint: Store the AutoIt script file and the AutoIt3.exe into your project folder.
012

Properties of Start Process activity
013

Hint: For the Inter Process Communication (IPC) with the AutoIt interpreter the clipboard is using. To synchronize the Start Process activity an Invoke Code activity is used.

Code for Invoke Code activity

'-Begin-----------------------------------------------------------------

Dim p() As Process

Do
  p = System.Diagnostics.Process.GetProcessesByName("AutoIt3")
  System.Threading.Thread.Sleep(500)
Loop Until p.Length = 0

'-End-------------------------------------------------------------------

Hint: To get the result from the AutoIt script the content of the clipboard is read.

Python

#-Begin-----------------------------------------------------------------

#-Includes--------------------------------------------------------------
import sys, win32com.client

#-Function test---------------------------------------------------------
def test():

  try:

    SapGuiAuto = win32com.client.GetObject("SAPGUI")
    if not type(SapGuiAuto) == win32com.client.CDispatch:
      return

    application = SapGuiAuto.GetScriptingEngine
    if not type(application) == win32com.client.CDispatch:
      SapGuiAuto = None
      return

    connection = application.Children(0)
    if not type(connection) == win32com.client.CDispatch:
      application = None
      SapGuiAuto = None
      return

    session = connection.Children(0)
    if not type(session) == win32com.client.CDispatch:
      connection = None
      application = None
      SapGuiAuto = None
      return

    session.findById("wnd[0]/tbar[0]/okcd").text = "/nSE16"
    session.findById("wnd[0]").sendVKey(0)
    session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").text = "TADIR"
    session.findById("wnd[0]").sendVKey(0)
    session.findById("wnd[0]").sendVKey(31)
    dbCount = session.findById("wnd[1]/usr/txtG_DBCOUNT").text
    session.findById("wnd[1]/tbar[0]/btn[0]").press()
    session.findById("wnd[0]").sendVKey(3)
    session.findById("wnd[0]").sendVKey(3)

  except:
    return sys.exc_info()[0]

  finally:
    session = None
    connection = None
    application = None
    SapGuiAuto = None

  return dbCount

#-End-------------------------------------------------------------------

Variables in UiPath
014

Sequence in UiPath

Hint: Store the Python script file into your project folder.
019

Properties of Load Python Script
016

Properties of Invoke Python Method
017

Properties of Get Python Object
018

Conclusion

As you can see, UiPath is a fantastic platform for integrating all scripting languages. It is also possible to realize integrative development scenarios with Scripting Tracker and UiPath. You can easily record and edit your script, besides the possibilities that UiPath offers, and integrate it into your workflow. This can be interesting for developers who have been using SAP GUI scripting with the Scripting Tracker for a long time and are now considering extending their automation scenarios with UiPath. For this case it could be good partners.
5 Likes

Hi Man, Could you tell how to work with Uipath and call SAP BAPI for sap operation . SAP GUI very good and i have been using from last 2yers. but i found BAPI with Uipath can do so much better. please help if you know howto useSAP BAPI & Uipath together. Thank you

@StefanSchnell thanks got answer of questions in your another post here… Unable to Install SAP connectors - #23 by balkishan

Thank you for sharing all this knowledge.

1 Like

hey @StefanSchnell do you have any tutorial to automate end to end and SAP based application ?

Hello @Prafull_B,
you can find different perspectives here in the forum or in the SAP Community. Standalone documents are not available.
Best regards
Stefan

2 Likes

hey @StefanSchnell thanks for your tips. but how to deploy script files(.vbs) to end robot users?

@Ivanco

Hello Ivanco,
put the file in your project folder and it will be part of your deployment.
Best regards
Stefan