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
Anyone know how I would do that with VB.NET and possibly without the need to launch the SAPGUI first?