Failing VBscript connection to SAP

Hi all,

I have a problem to connect to SAP via a VBscript. It works fine on my computer but does not for another user of the Robot. Scripting is enabled in SAP.

So the usual code to connect to SAP looks like this:

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

Again, for me this works fine, but for the other user an exception at

Set connection = application.Children(0)

so i tired to change is to

Set connection = application.OpenConnection("Our Correct SAP", True)

which at least established a connection and opened up the Session Manager but threw an error at the next part

Set session    = connection.Children(0)

so my next thought was maybe for some reason the Children count wasn´t 0.
So i stumbled upon @StefanSchnell way to itterate through the connections and sessions.
Again without success. It seems like for whatever reason there are no application.Children() and/or connection.Children().

Does anyone know what a reason for this problem could be? I´m grateful for every input.

Thanks!

Kind regards
Tim

PS: for reference, this is the itterative script i am talking about:
Edit: I just realized that this code wasn´t really helping since it utilizes a function. Nevertheless, this doesn´t change the outcome of the problem.


'-Function FindSAPWindowsByHandle---------------------------------------
Function FindSAPWindowByHandle(hSAPWnd)

  Dim SapGuiAuto, app, CollCon, oCon, CollSes, oSes, hWnd, i, j

  Set SapGuiAuto = GetObject("SAPGUI")
  If Not IsObject(SapGuiAuto) Then
    Exit Function
  End If

  Set app = SapGuiAuto.GetScriptingEngine
  If Not IsObject(app) Then
    Exit Function
  End If

  '-Get all connections-------------------------------------------------
  Set CollCon = app.Connections()
  If Not IsObject(CollCon) Then
    Exit Function
  End If

  '-Loop over connections-----------------------------------------------
  For i = 0 To CollCon.Count() - 1

    Set oCon = app.Children(CLng(i))
    If Not IsObject(oCon) Then
      Exit Function
    End If

    '-Get all sessions of a connection----------------------------------
    Set CollSes = oCon.Sessions()
    If Not IsObject(CollSes) Then
      Exit Function
    End If

    '-Loop over sessions------------------------------------------------
    For j = 0 To CollSes.Count() - 1

      Set oSes = oCon.Children(CLng(j))
      If Not IsObject(oSes) Then
        Exit Function
      End If 

      If oSes.Busy() = vbFalse Then

        hWnd = oSes.findById("wnd[0]").Handle
        
        If hSAPWnd = hWnd Then
          FindSAPWindowByHandle = oSes.ID
        End If

      End If

    Next

  Next

End Function

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

@Timsener

Hello Tim,
please take a look at TAC RZ11 and the parameter sapgui/user_scripting_per_user. If it is set to true, and other users as yours don’t have the authorization objects, it would explain exactly the behavior you describe. Furthermore, if the bot is running on another computer, you could still check the client-side SAP Logon settings to see if they are identical to those on your computer, to make sure it’s not that reason.
Best regards
Stefan

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.