0

So, I have a script created by another person using Excel Vba. The script is for assets creation and it should work for all SAP instances, but it doesn’t. For example, it works for an instance without any problem, but when I tried it in another one, it tells me:

User is already logged on in client

It is strange because I can open multiple instances manually, but not with the script. Note that I don’t connect with user and password. I already have an access and the only thing that I have to do in SAP Logon 760 is to mention the instance and press enter.

I would like to modifiy the script in order to resolve the problem and to understand easier the script,but I don’t know exactly how to do it:

  1. I want to check if SAP Logon 760 is open. If is not open, the script should open it, after that it should fill in the instance that I have mentioned in the specific cell in Excel, open the SAP window and complete the fields in transaction As01.
  2. If SAP Logon 760 is open, only open the instance and run As01.
  3. If the window for the instance is already opened, just run As01.
  4. If I have a window opened for another instance, the script should open another window with the instance that I want and run As01.

This is the code:

Sub As01()
'Declare variables for the transaction

 Dim SAPInstance As String  
    Dim SAPClient As String   
    Dim Mandant As String   
    Dim SAPClientLine As Integer 
    Dim SAPConnect As Integer

' If the SAP LOGON is not open, then open the SAPLOGON

SuiviErreur = 0
    On Error Resume Next
    If Not IsObject(SAP_applic) Then
        Set SapGuiAuto = GetObject("SAPGUI")
        Set SAP_applic = SapGuiAuto.GetScriptingEngine
    End If
    SuiviErreur = Err.Number
    If SuiviErreur <> 0 Then
        Call Shell("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus)
        Application.Wait (Now + TimeValue("00:00:05")) 
        Set SapGuiAuto = GetObject("SAPGUI")
        Set SAP_applic = SapGuiAuto.GetScriptingEngine
    End If

' If SAP windows are already open, then close them (15 max)

For x = 1 To 15
    Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
    Set SAPapp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
    Set SAPCon = SAPapp.Children(0) 'Get the first system that is currently connected
    Set session = SAPCon.Children(0) 'Get the first session (window) on that connectionsession.findById("wnd[0]").maximize
        session.findById("wnd[0]").Close
        session.findById("wnd[1]/usr/btnSPOP-OPTION1").press
   Next x 

If SAPInstance = "X" Then Set Connection = SAP_applic.OpenConnection("X : X, True)
If SAPInstance = "Y" Then Set Connection = SAP_applic.OpenConnection("Y: Y, True)
    'The code will continue with other instances

    Set SapGuiAuto = GetObject("SAPGUI")  
    Set SAPapp = SapGuiAuto.GetScriptingEngine 
    Set SAPCon = SAPapp.Children(0) 
    Set session = SAPCon.Children(0) 

' Case of instances where there is a choice on the client - We count by line in the choice list

SAPClientLine = 0
    Mandant = ""
    Mandant = session.findById("wnd[0]/usr/tblSAPMSYSTTC_IUSRACL/btnIUSRACL-MANDT[0," & SAPClientLine & "]").Text
    
    If Mandant = "" Then
        SAPConnect = 1
        Else
        SAPConnect = 0
    End If
    
    While SAPConnect = 0
        Mandant = session.findById("wnd[0]/usr/tblSAPMSYSTTC_IUSRACL/btnIUSRACL-MANDT[0," & SAPClientLine & "]").Text
        If SAPClient = Mandant Then
            session.findById("wnd[0]").maximize
            session.findById("wnd[0]/usr/tblSAPMSYSTTC_IUSRACL/btnIUSRACL-MANDT[0," & SAPClientLine & "]").SetFocus
            session.findById("wnd[0]/usr/tblSAPMSYSTTC_IUSRACL/btnIUSRACL-MANDT[0," & SAPClientLine & "]").press
            SAPConnect = 1
        End If
        SAPClientLine = SAPClientLine + 1
        If SAPClientLine > 6 Then
            SAPConnect = 1
            MsgBox ("Connection failed (" & SAPInstance & "/" & SAPClient & ")")
        End If
    Wend
    session.findById("wnd[0]").sendVKey 0
    'Here I don't understand exactly, because I don't have to select the client for connectiom
     ‘Enter As01… 

I hope that I am clear and thanks in advance for helping me

Lorena
  • 1
  • 1
  • Maybe [this](https://stackoverflow.com/a/71626050/6600940) helps. But, to be honest, you should re-factor the code. And IMHO you should always have the session started by the user themselves. BTW, you can add the client to `findGuiSession` easily, just look [here](https://help.sap.com/docs/sap_gui_for_windows/b47d018c3b9b45e897faf66a6c0885a8/15fb37a11e9143eb8eb0e5da85958422.html) – Storax Aug 10 '23 at 09:07

1 Answers1

0

I do not have Access to transaction code AS01 ;-) - But this code vill try with the logged on user to start SAP scripting system and find a fre sesson to use. If you are to perform automation in SAP scripting the best way is something like this:

Sub OpenAS01()
SystemName = "KO3"          'change as needed or use a variable
Transaction = "SESSION_MANAGER"      'change as needed or use a variable

On Error GoTo ErrorHandler:
If Not IsObject(Sap_Applic) Then
   Set SapGuiAuto = GetObject("SAPGUI")
   Set Sap_Applic = SapGuiAuto.GetScriptingEngine
End If
On Error GoTo 0
koniec:
qConnections = Sap_Applic.Connections.Count
If qConnections = 0 Then
    MsgBox "No connection to SAP"
    End
End If

'MsgBox Sap_Applic.Children(0).info.SystemName

bSession = False
For iConnectionCounter = 0 To qConnections - 1
    Set Connection = Sap_Applic.Children(Int(iConnectionCounter))
    'MsgBox Connection.Description
    'If Not Connection.Description = "" Then
    qSessions = Connection.Children.Count
        For iSessionCounter = 0 To qSessions - 1
            Set Session = Connection.Children(Int(iSessionCounter))
            'MsgBox Session.info.SystemName
        If Session.info.SystemName <> SystemName Then Exit For
            If Session.info.Transaction = Transaction Then
                bSession = True
                Exit For
            End If
        Next
    'End If
    If bSession Then Exit For
Next

If Not bSession Then
    MsgBox SystemName & " not available or free session not available"
    End
End If

Session.findById("wnd[0]").resizeWorkingPane 154, 24, False
Session.findById("wnd[0]/tbar[0]/okcd").Text = "AS01"
Session.findById("wnd[0]").sendVKey 0
' Do you code here

Exit Sub
ErrorHandler:
MsgBox "No connection to SAP"
End
End Sub