This website is not affiliated with, sponsored by, or approved by SAP AG.

SAP RFC Single-Sign-On/Automation

Interfaces (ALE, IDOC, RFC, EDI etc.)

Moderators: Snowy, thx4allthefish

SAP RFC Single-Sign-On/Automation

Postby bdabaum » Mon Apr 20, 2015 10:57 am

We have just converted to the SAP system. We have been developing a few automation tools using Excel and SAP Scripting. In order to do this, we would open our shortcut to the SAP application with SSO. Then execute the VBA script. However, now they are asking me to develop an application with VB.Net. I have referenced the libraries we used in the VBA and attempted to make similar API calls, but the SAP connections never return anything.

Here is some of the code we use to connect to the active SAPGUI.

Code: Select all
Private Function GetSAPSession() As Boolean
    On Error Resume Next ' Do this so we can inform the user that there is no login screen if GetObject fails
    Dim SapGuiAuto, SAPApp, connection, Ses, Utils, Info, Stop_Script
    Set SapGuiAuto = GetObject("SAPGUI")
    On Error GoTo AbortTheMission ' Now use normal error handling
    If Not IsObject(SapGuiAuto) Then
        MsgBox "Could not find SAP Logon window.", vbSystemModal + vbOKOnly + vbExclamation, "SAP Script Error"
        GetSAPSession = False
        Exit Function
    End If
   
    Set SAPApp = SapGuiAuto.GetScriptingEngine
    Set connection = SAPApp.Children(0)
   
    ' Go through each session and look for "SESSION_MANAGER" for a jumping off point
    For Each Ses In connection.Children
        If Ses.Busy = False Then
            If Ses.Info.Transaction = "SESSION_MANAGER" Then
                Set session = Ses
                Exit For
            End If
        End If
    Next
    ' make sure the SESSION_MANAGER was found
    If session Is Nothing Then  ' not found
        If connection.Children.Count = 6 Then ' all six available sessions were searched, and SESSION_MANAGER wasn't found
            Set Utils = SAPApp.Utils
            Utils.ShowMessageBox "Error Message", "Could not find an available session to execute script.", 2, 0
           
            Set session = connection.Children(0)
            Set Info = session.Info
            Stop_Script = MsgBox("You are currently in: " & Info.Transaction & vbCr & _
                "Do you want to use this connection?", vbYesNo + vbInformation, "Continue?")
       
            If Stop_Script = vbNo Then
                GoTo AbortTheMission
            End If
        Else ' SESSION_MANAGER isn't out there to be used, but there are enough connections available to start one
            ' create a new one
            connection.Children(0).CreateSession
            Application.Wait (Now + TimeValue("0:00:04"))
            ' find the one just created
            For Each Ses In connection.Children
                If Ses.Busy = False Then
                    If Ses.Info.Transaction = "SESSION_MANAGER" Then
                        Set session = Ses
                        Exit For
                    End If
                End If
            Next
        End If
    End If
    GetSAPSession = True
    Exit Function
AbortTheMission:
    GetSAPSession = False
End Function

The above code works in VBA but not in VB.NEt (vb.net code varies slightly using .net objects and classes)

Anyone know how I would do that with VB.NET and possibly without the need to launch the SAPGUI first?
bdabaum
 
Posts: 1
Joined: Mon Apr 20, 2015 9:52 am

Return to Interfaces

Who is online

Users browsing this forum: No registered users and 1 guest





loading...


This website is not affiliated with, sponsored by, or approved by SAP AG.