Hello. If you want to fill specific data in selection Screen automatically you Need to catch new SAP session called by functionmodule => adopt Excel VBA to this session => and then you can fill them via template.
I have an complex template Setup with different functions. One of them is a Loop over all existing SAP Sessions and get required data for adopt to VBA coding. In my template a listbox got filled where this function write data into an Sheet (SAP_CONN). I plan to enhance this in future so an Array got filled. But this is my current solution:
Public Function Listbox_1_SAP_SESSIONS() As Integer Dim SapGuiAuto As Object Dim i As Integer Dim iSession As Long Dim SAP_APP As Object Dim Connection As SAPFEWSELib.GuiConnection Dim Session As SAPFEWSELib.GuiSession Dim strSessions$ ThisWorkbook.Sheets("SAP_CONN").Range("A2:B30").ClearContents i = 1 ' There may be bad entries in the ROT from previous crashes While i < 10 And SapGuiAuto Is Nothing i = i + 1 On Error Resume Next Set SapGuiAuto = GetObject("SAPGUI") On Error GoTo 0 Wend If SapGuiAuto Is Nothing Then MsgBox "Could not connect to SAPlogon process. Did you start it?" Exit Function End If On Error Resume Next Set SAP_APP = SapGuiAuto.GetScriptingEngine Set SapGuiAuto = Nothing On Error GoTo 0 If SAP_APP Is Nothing Then MsgBox "Could not access GuiApplication. Maybe Scripting is disabled?" Exit Function End If Set SapGuiAuto = Nothing iSession = 0 For Each Connection In SAP_APP.Children If Not Connection.DisabledByServer Then For Each Session In Connection.Children If Session.Busy = False Then iSession = iSession + 1 ThisWorkbook.Sheets("SAP_CONN").Cells(iSession, 1).Value = (Session.Info.SystemName & " (" & CStr(Session.Info.SessionNumber) & ") (" & Session.Info.Client & ") | User: " & Session.Info.User & " | Transaction: " & Session.Info.Transaction) ThisWorkbook.Sheets("SAP_CONN").Cells(iSession, 2).Value = Session.ID End If Next End If Next Listbox_1_SAP_SESSIONS = iSession End Function
With an quite similar function I adopt to selected session (in Listbox on VBA userform):
Public Function objSession(strSessionID As String) As Object Dim SapGuiAuto As Object Dim SAP_APP As Object Dim i As Integer Dim bConnect As Boolean Dim Connection As SAPFEWSELib.GuiConnection Dim Session As SAPFEWSELib.GuiSession Set objSession = Nothing i = 1 ' There may be bad entries in the ROT from previous crashes While i < 10 And SapGuiAuto Is Nothing i = i + 1 On Error Resume Next Set SapGuiAuto = GetObject("SAPGUI") On Error GoTo 0 Wend If SapGuiAuto Is Nothing Then MsgBox "Please start SAPlogon" Exit Function End If On Error Resume Next Set SAP_APP = SapGuiAuto.GetScriptingEngine Set SapGuiAuto = Nothing On Error GoTo 0 If SAP_APP Is Nothing Then MsgBox "Scripting disabled" Exit Function End If Set SapGuiAuto = Nothing For Each Connection In SAP_APP.Children If Not Connection.DisabledByServer Then For Each Session In Connection.Children If Session.Busy = False Then If Session.ID = strSessionID Then Set objSession = Session locSessionHandle = Session.FindById("wnd[0]").Handle bConnect = True Exit For End If End If Next End If If bConnect = True Then Exit For Next End Function
You have now some Input how you can identify and adopt to SAP session called by RFC-called FM.
Br, Holger