0

It appears that I am running constantly in a problem that shouldn't exist as googleing it assumes that the code works. I try to simply run a SAP-transaction from Excel with VBA. I am logged into SAP, ready to issue the transaction. So the code connects to the GUI of SAP via the scripting engine and tries to grab the open session but fails:

Sub MVE
    Dim oSapGui As Object
    Dim oApp As SAPFEWSELib.GuiApplication
    Dim oConn As SAPFEWSELib.GuiConnection
    Dim oSession As SAPFEWSELib.GuiSession
    
    'Get the currently running SAP GUI
    Set oSapGui = GetObject("SAPGUI")
    Set oApp = oSapGui.GetScriptingEngine

    Debug.Print "oApp.Children.Count: " + Str(oApp.Children.Count)  'for debugging
    'Get the first system that is currently connected
    Set oConn = oApp.Children(0)

    Debug.Print "oConn.Children.Count: " + Str(oConn.Children.Count) 'for debugging
    'Get the first session (window) on that connection
    Set oSession = oConn.Children(0) '<-- Fails. There are no child objects, i.e. no sessions available
End Sub

The debugging output is:

oApp.Children.Count: 1
oConn.Children.Count: 0

and the corresponding error is obviously

Runtime error '614':
The enumerator of the collection cannot find an element with the specified index.

The code is stripped to the absolute necessary and many posts on the net suggest that it should work, but it doesn't (e.g. this on stackoverflow or this, this, or this in the SAP forums ). Scripting is enabled in both, SAP and Excel/VBA, as other posts identified as root cause.

scripting enabled Excel VBA SAP language is not English: scripting enabled SAP

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
max
  • 3,915
  • 2
  • 9
  • 25
  • Check the Locals window when stepping through the code is **oApp** indexed from zero or one? If it is, try changing `Set oConn = oApp.Children(0)` to `Set oConn = oApp.Children(1)` – Robert Mearns Jun 28 '22 at 10:16
  • thx @RobertMearns, it starts from 0. If a different index is used, provokes the same runtime error just earlier. So then `Set oConn = oApp.Children(1)` raises the exception because there is only one child as scripting engine – max Jun 28 '22 at 11:30
  • 1
    The only other thing to suggest is, triple check the scripting settings. The **sapgui/user_scripting** must be set to uppercase **TRUE**. Check the values for these settings: **sapgui/user_scripting_per_user**, **sapgui/user_scripting_disable_recording**, **sapgui/user_scripting_set_readonly**. Have a look at this document for more detail: http://www.sdn.sap.com/irj/scn/index?rid=/library/uuid/3099a575-9cf4-2a10-9492-9838706b9262&overridelayout=true – Robert Mearns Jun 28 '22 at 12:15
  • We see your screenshots about your SAP GUI installation, but you didn't post a screenshot about the SAP GUI menu Options > Script Recording and Playback, which indicates if the server allows Scripting (`sapgui/user_scripting`, etc.) Grayed out = disabled by server. – Sandra Rossi Jun 28 '22 at 15:33
  • Oh I see, the menu *Options > Script Recording and Playback* is indeed grayed out, @SandraRossi. I don't know how to check the other parameters as I have no right for the transactions RZ11 and SPAM – max Jun 28 '22 at 15:49
  • Ask the administrator, or workaround is to run `SA38`/`SE38` program `RSPARAM`; other possibility is to look directly at the profile parameter file(s) via `AL11` if you can find the profile folder. – Sandra Rossi Jun 28 '22 at 18:22

0 Answers0