'Trouble Connecting to SAP Through VBA in Excel Error 614
I'm trying to run a basic code I've found numerous places on here and elsewhere which should open SAP, input a command, and execute.
I've gone as far as SAP opening through VBA but I get runtime error 614 at line Set Connection = Appl.Openconnection("1) PRD", True).Children(0)
below, in regard to setting the "session" as shown.
Run-Time Error 614: The enumerator of the collection cannot find en element with the specified index
Private Sub CommandButton1_Click()
Dim SapGui As Object
Dim Connection As Object
Dim Appl As Object
Dim session As Object
Dim WshShell As Object
'Of course change for your file directory
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) PRD", True).Children(0)
Set session = Connection.Children(0)
session.findById("wnd[0]").Maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "cv03n"
session.findById("wnd[0]").sendVKey 0 'ENTER
'and there goes your code in SAP
End Sub
Solution 1:[1]
Please check the name of your SAP connection. According to the example below, a corresponding command would look like this:
...
Set Connection = Appl.openconnection("DAL_ EHP5")
Set session = Connection.Children(0)
...
Furthermore, it may be possible that you first have to log into SAP.
for example:
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = Mandant
session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = Name
session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = PW
session.findById("wnd[0]").sendVKey 0
Regards, ScriptMan
Solution 2:[2]
This error may happen on accessing the GuiConnection
object if the SAP system is configured to prevent the execution of scripts with SAP GUI Scripting.
Ask the SAP administrator to enable scripts (profile parameter sapgui/user_scripting
to be set to TRUE
; other possibilities are described in SAP KBA SAP Knowledge Base Article 2296251 - How can SAP GUI Scripting be enabled?).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ScriptMan |
Solution 2 | Sandra Rossi |