I am making a Userform with several Optionbuttons, to generate different SAP reports automatically. According to the user's selection, a specific report is generated.
But I notice that I am repeating the SAP connection code multiple times (for each radio button). I am repeating the following code in each sub that I have.
Dim Appl, SapGuiAuto, Connection, session As Object
Call Shell("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbMinimizedFocus)
Esperar 4 'Esperar unos segundos hasta que se abra el SAP
If Not IsObject(Appl) Then
Set SapGuiAuto = GetObject("SAPGUI")
Esperar 3
Set Appl = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = Appl.Openconnection("Producción ERP LatAm [EP0]", True)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
Else
Exit Sub
End If
I wanted to know if there is any way to create a sub for the SAP connection considering public variables or something like that. I want to avoid repeating code. Thank you so much