0
votes

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
1
What does Debug.Print Connection.Children.Count return in the immediate window if you put it before Set session = Connection.Children(0)?Nacorid

1 Answers

0
votes

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

SAP Logon