Skip to Content
0

How can I connect to SAP with a script?

Mar 24, 2017 at 06:07 PM

378

avatar image

Hi there,

In order to automate some data extraction from SAP APO (SAP GUI), we have tried with my team to write a script in Visual Basic to perform the data extraction. The native function in SAP of script recording and playback looks very promising but we are still trying to figure out how to connect first to SAP through a script.

I have recently found a VB script to do that (I will paste the script below my post), but I keep getting an error saying that my server is not found: "Hostname 'XXX' unknown". Even if I take the 3-character code from my SAP Logon servers' list.

Do you have any suggestions concerning the issue?

Thanks!

Automatic connection script:

REM The following script was written to log into the SAP server automatically.

REM To view historical information and credit for this script please see

REM the following thread on the SAP Community Network:

REM http://scn.sap.com/thread/3763970

REM This script was last updated by Paul Street on 7/1/15

REM Directives

Option Explicit

REM Variables! Must declare before using because of Option Explicit

Dim WSHShell, SAPGUIPath, SID, InstanceNo, WinTitle, SapGuiAuto, application, connection, session

REM Main

Set WSHShell = WScript.CreateObject("WScript.Shell")

If IsObject(WSHShell) Then

REM Set the path to the SAP GUI directory

SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\"

REM Set the SAP system ID

SID = "XXX"

REM Set the instance number of the SAP system

InstanceNo = "14"

REM Starts the SAP GUI

WSHShell.Exec SAPGUIPath & "SAPgui.exe " & SID & " " & _

InstanceNo

REM Set the title of the SAP GUI window here

WinTitle = "SAP"

While Not WSHShell.AppActivate(WinTitle)

WScript.Sleep 250

Wend

Set WSHShell = Nothing

End If

REM Remove this if you need to test the above script and want a message box at the end launching the login screen.

REM MsgBox "Here now your script..."

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

session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "USERNAME" session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "PASSWORD" session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 10 session.findById("wnd[0]").sendVKey 0

10 |10000 characters needed characters left characters exceeded

Hi M'hamed,

Welcome to the SAP Community!

I adjusted the tags slightly on your question. I believe the "logging" tags you had selected are about something different -- i.e., logging activities, not logging on. I think "SAP Gateway for Microsoft" may be most relevant, so I put that in place.

Cheers,
Matt

0

Thanks for your help!

0
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Stefan Schnell Mar 27, 2017 at 08:23 PM
0

Hello M'hamed,

please take a look here, you can find in the course of the discussion different solutions to connect an SAP system.

Here is another solution:

'-Begin-----------------------------------------------------------------
'-
'- VBScript to start SAP Logon if process doesn't exists and connect
'- to an SAP system. The SAP Logon process is detected via WMI
'- (Windows Management Instrumentarium), also the termination.
'-
'-----------------------------------------------------------------------

  '-Directives----------------------------------------------------------
    Option Explicit

  '-Variables-----------------------------------------------------------
    Dim SAPLogon, SAPLogonTitle
    SAPLogon = "saplogon.exe" : SAPLogonTitle = "SAP Logon 740"
    'SAPLogon = "saplgpad.exe" : SAPLogonTitle = "SAP Logon Pad 740"
    Dim SysDescr, SysIP
    SysDescr = "NSP" : SysIP = "192.168.203.134"

  '-Function FindProcess------------------------------------------------
    Function FindProcess(ProcessName)

      '-Variables-------------------------------------------------------
        Dim WMIServ, Processes, Process

      FindProcess = False

      Set WMIServ = GetObject("winmgmts:{impersonationLevel=" & _
        "impersonate}!\\.\root\cimv2") 

      Set Processes = WMIServ.ExecQuery("Select * from Win32_Process " & _
        "Where Name = '" & ProcessName & "'")

      For Each Process In Processes
        FindProcess = True
        Exit Function
      Next
    
    End Function

  '-Sub TerminateProcess------------------------------------------------
    Sub TerminateProcess(ProcessName)

      '-Variables-------------------------------------------------------
        Dim WMIServ, Processes, Process

      Set WMIServ = GetObject("winmgmts:{impersonationLevel=" & _
        "impersonate}!\\.\root\cimv2") 

      Set Processes = WMIServ.ExecQuery("Select * from Win32_Process " & _
        "Where Name = '" & ProcessName & "'")

      For Each Process In Processes
        Process.Terminate()
        Exit Sub
      Next

    End Sub

  '-Function GetSAPGUIObject--------------------------------------------
  '-
  '- This function starts the SAP Logon if it is necessary and delivers
  '- its object
  '-
  '---------------------------------------------------------------------
    Function GetSAPGUIObject()

      '-Variables-------------------------------------------------------
        Dim WshShell, Exec

      If FindProcess(SAPLogon) Then
        Set GetSAPGUIObject = GetObject("SAPGUI")
      Else
        Set WshShell = CreateObject("WScript.Shell")
        Set Exec = WshShell.Exec(_
          "c:\Program Files (x86)\SAP\FrontEnd\SAPgui\" & SAPLogon)
        Do While Not WshShell.AppActivate(SAPLogonTitle) 
          WScript.Sleep 500 
        Loop
        Set GetSAPGUIObject = GetObject("SAPGUI")
      End If

    End Function

  '-Sub Main------------------------------------------------------------
    Sub Main()

      '-Variables-------------------------------------------------------
        Dim SapGuiAuto, Application, Connection, Session

      Set SapGuiAuto = GetSAPGUIObject()
      If Not IsObject(SapGuiAuto) Then
        Exit Sub
      End If

      Set Application = SapGuiAuto.GetScriptingEngine
      If Not IsObject(Application) Then
        Set SapGuiAuto = Nothing
        Exit Sub
      End If

      '-----------------------------------------------------------------
      '-
      '- Here you have the possibility to decide which way you want to
      '- use, on the one hand via system description or on the other
      '- hand via IP address
      '-
      '-----------------------------------------------------------------
      'Set Connection = Application.OpenConnection(SysDescr, True)
      Set Connection = Application.OpenConnectionByConnectionString(_
        "/H/" & SysIP, True)
      If Not IsObject(Connection) Then
        Set Application = Nothing
        Set SapGuiAuto = Nothing
        Exit Sub
      End If

      Set Session = Connection.Children(0)
      If Not IsObject(Session) Then
        Set Connection = Nothing
        Set Application = Nothing
        Set SapGuiAuto = Nothing
        Exit Sub
      End If

      MyScript(Session)

      Set Session = Nothing
      Set Connection = Nothing
      Set Application = Nothing
      Set SapGuiAuto = Nothing
      TerminateProcess SAPLogon

    End Sub

  '-Sub MyScript--------------------------------------------------------
    Sub MyScript(Session)

      '-Your script here------------------------------------------------
      session.findById("wnd[0]").maximize
      session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "USERNAME"
      session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "PASSWORD"
      session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus
      session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 10
      session.findById("wnd[0]").sendVKey 0

    End Sub

  '-Main----------------------------------------------------------------
    Main

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

Let us know your results.

Cheers
Stefan

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

I have run a few tests on your script, and it looks like it's working! I do not have any error message concerning the connection, and the script makes it way to the SAP main page, from where I can execute transactions and do other operations.

Thank you Stefan for your contribution!

1