Skip to Content
0

SAP Scripting

Sep 05, 2017 at 02:29 PM

291

avatar image
Former Member

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

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

2 Answers

Best Answer
Stefan Schnell
Sep 06, 2017 at 02:42 PM
0

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

Show 3 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Thanks Stefan for explaining the code. Could you pls assist me with below code and point out where i am going wrong here?

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 be executed in case Logon pad is not running, where i open logon pad with below commands, currently as i understand my code is not going beyond If statement and throws syntax error)

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......

0

Hello Dipesh,

try this way:

On Error Resume Next
Set SapGuiAuto = GetObject("SAPGUI")
On Error Goto 0
If Not IsObject(SapGuiAuto) Then
  Set WshShell = CreateObject("WScript.Shell")
  Set proc = WshShell.Exec("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe")
  WScript.Sleep 4000
  Set SapGuiAuto = GetObject("SAPGUI")
End If
Set application = SapGuiAuto.GetScriptingEngine
SID = InputBox("Please enter SID to login")

Cheers
Stefan

0
Former Member
Stefan Schnell

Thanks a ton Stefan, your code did the work for me. It is working as expected...

Now i can add more code for my task and complete the script.

Thanks again :)

1
Script Man Sep 06, 2017 at 06:33 AM
0

How about "true" or "false"?

Regards,

ScriptMan

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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......

0