Skip to Content

SAP Scripting

When we try to use scripting and recording , we get below code which comes as part of any script you create in SAP GUI. What values does If statements give and how does this work in background?

If Not IsObject(application) Then (What will be the value returned if SAP logonpad is open and when not open?)

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Sep 06, 2017 at 02:42 PM

    Hello Dipesh,

    it is very easy to understand.

    If you start the saplogon.exe it creates an object called SAPGUI on your system. With the command
    Set SapGuiAuto = GetObject("SAPGUI")
    you get the interface of the SAPGUI object.

    With the command
    Set application = SapGuiAuto.GetScriptingEngine
    you get the interface for the currently running SAP GUI process.

    With the commands
    Set connection = application.Children(0) and Set session = connection.Children(0)
    you connect to the first connection and the first session. A session is a visible screen.

    If you start your script all the variables from SapGuiAuto to session must be set, otherwise it is not possible to work with a session. The If Not construct is not really necessary, you can also use this

      Set SapGuiAuto = GetObject("SAPGUI")
      Set application = SapGuiAuto.GetScriptingEngine
      Set connection = application.Children(0)
      Set session = connection.Children(0)
    

    Or if you want to be sure that all works well this

    '-Begin-----------------------------------------------------------------
    
      '-Directives----------------------------------------------------------
      Option Explicit
    
      '-Sub Main------------------------------------------------------------
      Sub Main()
    
        '-Variables---------------------------------------------------------
        Dim SapGuiAuto, application, connection, session
    
        Set SapGuiAuto = GetObject("SAPGUI")
        If Not IsObject(SapGuiAuto) Then
          Exit Sub
        End If
    
        Set application = SapGuiAuto.GetScriptingEngine
        If Not IsObject(application) Then
          Exit Sub
        End If
    
        Set connection = application.Children(0)
        If Not IsObject(connection) Then
          Exit Sub
        End If
    
        Set session = connection.Children(0)
        If Not IsObject(session) Then
          Exit Sub
        End If
    
        "-Here your script--------------------------------------------------
    
    
    
        Set session = Nothing
        Set connection = Nothing
        Set application = Nothing
        Set SapGuiAuto = Nothing
    
      End Sub
    
      '-Main----------------------------------------------------------------
      Main
    
    '-End-------------------------------------------------------------------
    

    You can find here a very interesting document, look at page 11 at Top Level Administrative Objects, it is a very good explanation. Also you can find here a very good introduction.

    Cheers
    Stefan

    Add comment
    10|10000 characters needed characters exceeded

  • Sep 06, 2017 at 06:33 AM

    How about "true" or "false"?

    Regards,

    ScriptMan

    Add comment
    10|10000 characters needed characters exceeded

    • I know If loop will provide True or false :), i was looking for concrete answers but anyway...

      I read your comments on few other questions related to Scripting and was wondering if you could assist me with my code.

      I am trying to work towards automating a task using SAP script and everything is setup except SAP logon.

      My script works fine when SAPlogon.exe (logon pad) is already open. I am trying a workaround where my script opens SAP logon pad when SAP logon pad is not running and then rest of my script kicks in and perform its task.

      Here is my code...

      If Not IsObject(application) Then (usually any sap script will start with this code and mine also works if logon pad is open beforehand)

      Set SapGuiAuto = GetObject("SAPGUI") (if logon pad is not open and i execute the script i get Incorrect Syntax at this line)

      Set application = SapGuiAuto.GetScriptingEngine

      SID = InputBox("Please enter SID to login") (Here i am asking user to enter SID of system on which they want to login)

      Else (Below code should kick in case Logon pad is not running, where i open logon pad with below commands)

      Set WshShell = CreateObject("WScript.Shell")

      MsgBox "Looks like SAPlogon is not started, Do you want to start?" (Just for fun)

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

      WScript.Sleep 4000

      Set SapGuiAuto = GetObject("SAPGUI")

      Set application = SapGuiAuto.GetScriptingEngine

      SID = InputBox("Please enter SID to login")

      End If

      rest of script......