;-Begin-----------------------------------------------------------------
;-
;- Program to start the SAP logon, SAP GUI and object navigator
;-
;-----------------------------------------------------------------------

  ;-Include-------------------------------------------------------------
    #Include "..\Include\GuiListView.au3"

  ;-Variables-----------------------------------------------------------
    Dim $UserName = "BCUSER"
    Dim $PassWord = "minisap"
    Dim $SystemDescription = "NSP lokal"

    ;-LogonTitle--------------------------------------------------------
      Dim $LogonWinTitle = "SAP Logon Pad 710"
      Dim $LogonWinClass = "#32770"
      Dim $LogonTitle = "[TITLE:" & $LogonWinTitle & _
        "; CLASS:" & $LogonWinClass & "]"
   
    ;-SAPTitle----------------------------------------------------------
      Dim $SAPStartWinTitle = "SAP"
      Dim $SAPEasyAccessWinTitle = "SAP Easy Access"
      Dim $SAPWinClass = "SAP_FRONTEND_SESSION"
      Dim $SAPStartTitle = "[TITLE:" & $SAPStartWinTitle & _
        "; CLASS:" & $SAPWinClass & "]"
      Dim $SAPEasy = "[TITLE:" & $SAPEasyAccessWinTitle & _
        "; CLASS:" & $SAPWinClass & "]"

  ;-Procedure StartSAPGUI-----------------------------------------------
    Func StartSAPGUI()
  
      Dim $System, $hListView
      Dim $ListView = "[CLASS:SysListView32; CLASSNN:SysListView321]"
      Dim $Transaction = "[CLASS:Edit; CLASSNN:Edit1]"
      Dim $SAPLogon = Run("C:\\Programme\\SAP\\FrontEnd\\SAPgui\\saplgpad.exe", _
        "C:\\Programme\\SAP\\FrontEnd\\SAPgui")
  
      If $SAPLogon Then
        ;-Wait for MMC window-------------------------------------------
          WinWaitActive($LogonTitle)
        ;-Select the NSP entry------------------------------------------
          $System = ControlListView($LogonTitle, "", $ListView, _
            "FindItem", $SystemDescription)
          If $System > -1 Then
            ControlListView($LogonTitle, "", $ListView, "Select", _
              $System)  
            $hListView = ControlGetHandle ($LogonTitle, "", $ListView)
            _GUICtrlListView_SetItemSelected($hListView, $System, _
              True, True)
            Send("{ENTER}")
            ;-Wait for SAPGUI-------------------------------------------
              WinWaitActive($SAPStartTitle)
              If WinExists($SAPStartTitle) Then
                Send($UserName & "{TAB}" & $PassWord & "{ENTER}")
                ;-Wait for SAP Easy Access------------------------------
                  WinWaitActive($SAPEasy)
                ;-Start object navigator--------------------------------
                  ControlSetText($SAPEasy, "", $Transaction, "se80")
                  Send("{ENTER}")
              EndIf   
            ;-Close LogonPad--------------------------------------------
              WinActivate($LogonTitle)
              WinKill($LogonTitle)   
          EndIf
      EndIf
   
    EndFunc
  
  ;-Main----------------------------------------------------------------
    StartSAPGUI()

;-End-------------------------------------------------------------------