cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Connection.Children.Count returns Object variable not set (Error 91)

0 Likes
2,202

This is the code that I've been able to create with the help of the internet, here I'm able to see if there's a connection opened in SAP and if not, It'll use my username and pass to login.

My problem lies where I try to check how many sessions is opened already since the max number of sessions I can work with is only 5, so if it 4 or less, I want to open a new session, but it fails with error:

Object variable not set (Error 91)

at line:

<code>ChildCount = Connection.Children.Count

Complete code:

<code>Sub Login_SAP()

Dim Appl As Object
Dim Connection As Object
Dim session As Object
Dim WshShell As Object
Dim SapGui As Object
Dim login As String
Dim password As String
Dim SAPConn         As SAPFEWSELib.GuiConnection
Dim SAPApp          As SAPFEWSELib.guiApplication
Dim SAPConnColl     As SAPFEWSELib.GuiComponentCollection
Dim SAPSessColl     As SAPFEWSELib.GuiComponentCollection
Dim SapGuiAuto      As Object


Set SapGuiAuto = GetObject("SAPGUI")
Set SAPApp = SapGuiAuto.GetScriptingEngine    'connects in the SAP that is running at the moment

On Error GoTo needlogin 'if error, it means that SAP is not connected to anything
Set SAPCon = SAPApp.Children(0)               'find the first system that is connected
Set session = SAPCon.Children(0)              'find the first session of this

'if the code arrived here, it will assume that there's a logon running already, so it will jump to the login
GoTo transaction

'my file directory
needlogin:


Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", 4
Set WshShell = CreateObject("WScript.Shell")
Do Until WshShell.AppActivate("SAP Logon ")
    Application.Wait Now + TimeValue("0:00:01")
Loop
Set WshShell = Nothing
Set SapGui = GetObject("SAPGUI")
Set Appl = SapGui.GetScriptingEngine
Set Connection = Appl.OpenConnection("1.1 - Sap ECC Leader Produção.", True)
Set session = Connection.Children(0)


'my username and password
session.FindById("wnd[0]/usr/txtRSYST-MANDT").text = "400"
session.FindById("wnd[0]/usr/txtRSYST-BNAME").text = "l94803"
session.FindById("wnd[0]/usr/pwdRSYST-BCODE").text = "805894"
session.FindById("wnd[0]/usr/txtRSYST-LANGU").text = "pt"

session.FindById("wnd[0]").sendVKey 0 'Confirmation

transaction:
'===============================================================================================
'HERE I'M GETTING AN ERROR NUMBER 91

ChildCount = Connection.Children.Count
If ChildCount <= 5 And ChildCount >= 1 Then
    session.CreateSession
End If
        
'===============================================================================================
'BELOW ALL OF MY CURRENTLY CODE THAT IS RUNNING PERFECTLY

End Sub

Accepted Solutions (0)

Answers (0)