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:
- 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
. - If SAP Logon 760 is open, only open the instance and run
As01
. - If the window for the instance is already opened, just run
As01
. - 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