Open SAP from Excel VBA

0 votes

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
Sep 29, 2022 in Others by Kithuzzz
• 38,010 points
3,629 views

1 answer to this question.

0 votes
It just means that there is no sap session with the index 0. Check if you started the SAPLOGON Pad. Then check if you have connected to an SAP system.
answered Sep 30, 2022 by narikkadan
• 63,420 points

Related Questions In Others

0 votes
1 answer

How to open .xlsx files in MS Excel from VS Code?

Hello, to open xlxs files, or files ...READ MORE

answered Feb 17, 2022 in Others by gaurav
• 23,260 points
2,955 views
0 votes
1 answer
0 votes
1 answer

Runtime error 438 while importing data in excel from secured website using VBA

Replace With ieDoc.forms(0) .userType.Value = "1" ...READ MORE

answered Sep 23, 2022 in Others by narikkadan
• 63,420 points
650 views
0 votes
1 answer

Remove formulas from all worksheets in Excel using VBA

Try this : Option Explicit Sub test1() ...READ MORE

answered Oct 3, 2022 in Others by narikkadan
• 63,420 points
1,385 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
864 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,167 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
460 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,670 points
719 views
0 votes
1 answer

VBA Switch from an open excel app to a different open Excel app

Switch Between Excel Instances Using AppActivate After app-activating, you ...READ MORE

answered Mar 30, 2023 in Others by narikkadan
• 63,420 points
235 views
0 votes
1 answer

Excel VBA if file closed, then open and paste, else just paste data

Slightly re-worked to add full workbook/sheet qualifiers ...READ MORE

answered Sep 21, 2022 in Others by narikkadan
• 63,420 points
538 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP