title: "connection.Children(0)" triggers "The enumerator of the collection cannot find en element with the specified index."
body: <p>I am using a VBScript to login automatically into SAP GUI.
  It opens automatically the SAP GUI window, it loads the SAP server, but it doesn't populate automatically the user and password fields (remain blank).</p>
  <p>It also gives a script error on line 52, char 4:</p>
  <blockquote>
  <p>The enumerator of the collection cannot find en element with the specified index.</p>
  </blockquote>
  <p>The code is the following:</p>
  <pre><code>REM The following script was written to log into the SAP server automatically.
  REM To view historical information and credit for this script please see
  REM the following thread on the SAP Community Network:
  REM http://scn.sap.com/thread/3763970
  
  REM This script was last updated by Paul Street on 7/1/15
  
  REM Directives
      Option Explicit
  
    REM Variables!  Must declare before using because of Option Explicit
      Dim WSHShell, SAPGUIPath, SID, InstanceNo, WinTitle, SapGuiAuto, application, connection, session
  
    REM Main
      Set WSHShell = WScript.CreateObject(&quot;WScript.Shell&quot;)
      If IsObject(WSHShell) Then
  
        REM Set the path to the SAP GUI directory
          SAPGUIPath = &quot;C:\Program Files (x86)\SAP\FrontEnd\SAPgui\&quot;
  
        REM Set the SAP system ID
          SID = &quot;eaiserver.domain.com&quot;
  
        REM Set the instance number of the SAP system
          InstanceNo = &quot;38&quot;
  
        REM Starts the SAP GUI
          WSHShell.Exec SAPGUIPath &amp; &quot;SAPgui.exe &quot; &amp; SID &amp; &quot; &quot; &amp; _
            InstanceNo
   
        REM Set the title of the SAP GUI window here
          WinTitle = &quot;SAP&quot;
   
        While Not WSHShell.AppActivate(WinTitle)
          WScript.Sleep 250
        Wend
   
        Set WSHShell = Nothing
      End If
   
      REM Remove this if you need to test the above script and want a message box at the end launching the login screen.
      REM MsgBox &quot;Here now your script...&quot; 
       
      If Not IsObject(application) Then
         Set SapGuiAuto  = GetObject(&quot;SAPGUI&quot;)
         Set application = SapGuiAuto.GetScriptingEngine
      End If
      If Not IsObject(connection) Then
         Set connection = application.Children(0)
      End If
      If Not IsObject(session) Then
         Set session    = connection.Children(0)
      End If
      If IsObject(WScript) Then
         WScript.ConnectObject session,     &quot;on&quot;
         WScript.ConnectObject application, &quot;on&quot;
      End If
      session.findById(&quot;wnd[0]&quot;).maximize
      session.findById(&quot;wnd[0]/usr/txtRSYST-MANDT&quot;).Text = &quot;100&quot;
      session.findById(&quot;wnd[0]/usr/txtRSYST-BNAME&quot;).text = &quot;I'veInsertedtheCorrectUsernameHere&quot;
      session.findById(&quot;wnd[0]/usr/pwdRSYST-BCODE&quot;).text = &quot;I'veInsertedtheCorrectPassHere&quot;
      session.findById(&quot;wnd[0]/usr/txtRSYST-LANGU&quot;).Text = &quot;PT&quot;
      session.findById(&quot;wnd[0]/usr/pwdRSYST-BCODE&quot;).setFocus
      session.findById(&quot;wnd[0]/usr/pwdRSYST-BCODE&quot;).caretPosition = 10
      session.findById(&quot;wnd[0]&quot;).sendVKey 0
  </code></pre>
  <p>Thanks for the help!</p>