I have the same error as How to run an SAP transaction with SAP closed or open or not logged in. SAP-Logon and SAP are opened and then comes the error.
run-time error 614. 'The enumerator of the collection cannot find an element with the specified index'
Error always comes on the line:
Set Session = Connection.Children(0)
My SAP-Version with Single Sign-On:
SAP Logon für Windows
760 Final Release
7600.1.6.1160
1998402
Here is my code:
Sub test()
'Launching SAP and logging into the main screen if the program is not already open
If IsProcessRunning("saplogon.exe") = False Then
Dim SapGui, Applic, connection, session, WSHShell, myError, WScript
Shell "C:\Program Files (x86)\SAP\FrontEnd\SapGui\saplogon.exe", vbNormalFocus
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 Applic = SapGui.GetScriptingEngine
Set connection = Applic.OpenConnection("050 LYB PRD - SAP Production", True)
'error comes here if SAP-Logon and SAP is not logged in
Set session = connection.Children(0)
session.findById("wnd[0]").maximize
session.findById("wnd[0]").sendVKey 0
End If
'Executing main screen, this part here is just setting variables for the logged in interface. So once this is done you can enter the transaction.
'-----------new--------------------------
On Error Resume Next
'-----------new--------------------------
If Not IsObject(XXX) Then
Set SapGui = GetObject("SAPGUI")
Set XXX = SapGui.GetScriptingEngine
'-----------new--------------------------
myError = Err.Number
'-----------new--------------------------
End If
If Not IsObject(connection) Then
Set connection = XXX.Children(0)
'-----------new--------------------------
myError = Err.Number
'-----------new--------------------------
End If
If Not IsObject(session) Then
'error comes here if SAP-Logon is open and SAP is connected
Set session = connection.Children(0)
'-----------new--------------------------
myError = Err.Number
'-----------new--------------------------
End If
'-----------new--------------------------
On Error GoTo 0
If myError <> 0 Then
Set connection = XXX.OpenConnection("050 LYB PRD - SAP Production", True)
'error comes here if only SAP-Logon is open and SAP not connected
Set session = connection.Children(0)
session.findById("wnd[0]").maximize
End If
'-----------new--------------------------
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject Applic, "on"
End If
'Transaction comes here
End Sub
Function IsProcessRunning(process As String)
Dim objList As Object
Set objList = GetObject("winmgmts:") _
.ExecQuery("select * from win32_process where name='" & process & "'")
IsProcessRunning = objList.Count > 0
End Function