[SAP] SAP GUI Scripting per VBA

Voraussetzungen

  • System ab SAP NW 6.2
    • Präsentationsserver: SAP Menu (SAP Easy Access) -> Button “Lokales Layout anpassen” -> Optionen… -> Barrierefreiheit und Scripting -> Skiptunterstützung aktivieren, die anderen Haken deaktivieren
    • Applikationsserver: Profilparameter: RZ11 -> Parameter sapgui/user_scripting = TRUE (in Großbuchstaben!) -> gilt nur ab dem nächsten Login und bis Serverneustart
      • weitere Profilparameter:
        • sapgui/user_scripting must be TRUE to use scripting functions
        • sapgui/user_scripting_disable_recording if it is TRUE then it is only possible to run scripts
        • sapgui/user_scripting_force_notification if it is TRUE a notification is always displayed at the frontend
        • sapgui/user_scripting_per_user if it is TRUE you need S_SCR authority to use scripting
        • sapgui/user_scripting_set_readonly if it is TRUE it is not allowed to change the state of SAP GUI

Skriptaufzeichnung und Hilfe

  • SAP Menu (SAP Easy Access) -> Button “Lokales Layout anpassen” -> Skript-Aufzeichnung und -Playback
  • SAP Menu (SAP Easy Access) -> Button “Lokales Layout anpassen” -> Hilfe zu SAP GUI Scripting

OCX-Bibliothek für VBA

  • VBA-Import über: Extras -> Verweise … -> Durchsuchen …
  • C:\Program Files\SAP\FrontEnd\SAPgui\sapfewse.ocx
    oder
    C:\Apps32\SAP\FrontEnd\SAPgui\sapfewse.ocx
  • Aktivieren: SAP GUI Scripting API

Windows Scripting Host (x64)

  • C:\Windows\SysWOW64\wscript.exe
  • Ausführen von vbs-Scripten: C:\Windows\SysWOW64\wscript.exe c:\test.vbs

Beispiel 1 (Alle Connections, Sessions, GUI-Elemente auflisten)

Option Explicit

Sub SAP()
    Dim oSapGui As Object
    ' Extras -> Verweise ... -> Durchsuchen ...
    ' C:\Program Files\SAP\FrontEnd\SAPgui\sapfewse.ocx
    ' Aktivieren: SAP GUI Scripting API
    Dim oApp As SAPFEWSELib.GuiApplication
    Dim oConn As SAPFEWSELib.GuiConnection
    Dim oSession As SAPFEWSELib.GuiSession
    Dim oSessionInfo As SAPFEWSELib.GuiSessionInfo
    Dim oWindow As SAPFEWSELib.GuiMainWindow
    Dim oComponent As SAPFEWSELib.GuiComponent
    Dim oSubComponent As SAPFEWSELib.GuiComponent
    
    Debug.Print "----------"
    
    ' Ref auf SAPGUI
    Set oSapGui = GetObject("SAPGUI")
    If IsObject(oSapGui) Then
        ' Ref auf ScriptingEngine
        Set oApp = oSapGui.GetScriptingEngine
        If IsObject(oApp) Then
            
            Debug.Print oApp.ID
            Debug.Print oApp.Name
            
            ' Liste Connections
            For Each oConn In oApp.Children
                Debug.Print "|--" & oConn.ConnectionString
                Debug.Print "|  " & oConn.Description
                Debug.Print "|  " & oConn.ID
                Debug.Print "|  " & oConn.Name
                
                ' Sessions sind nur verfügbar, wenn RZ11 Parameter sapgui/user_scripting = TRUE
                ' Liste Sessions
                For Each oSession In oConn.Children
                    Debug.Print "  |--" & oSession.ID
                    Debug.Print "  |  " & oSession.Busy
                    
                    ' Ref auf Session Info
                    Set oSessionInfo = oSession.Info
                    
                    If IsObject(oSessionInfo) Then
                        Debug.Print "    |--" & oSessionInfo.SystemSessionId
                        Debug.Print "    |  " & oSessionInfo.ApplicationServer
                        Debug.Print "    |  " & oSessionInfo.SystemName
                        Debug.Print "    |  " & oSessionInfo.Client
                        Debug.Print "    |  " & oSessionInfo.Transaction
                        Debug.Print "    |  " & oSessionInfo.User
                    End If
                    
                    ' Ref auf Fenster
                    For Each oWindow In oSession.Children
                        Debug.Print "      |--" & oWindow.ID
                        Debug.Print "      |  " & oWindow.Text
                        Debug.Print "      |  " & oWindow.Left
                        Debug.Print "      |  " & oWindow.Top
                        Debug.Print "      |  " & oWindow.Width
                        Debug.Print "      |  " & oWindow.Height
                        
                        ' Liste mit Komponenten im Fenster
                        For Each oComponent In oWindow.Children
                            Debug.Print "        |--" & oComponent.ID
                            Debug.Print "        |  " & oComponent.Name
                            Debug.Print "        |  " & oComponent.Type
                            Debug.Print "        |  " & oComponent.ContainerType
                            
                            ' Liste mit Unterkomponenten (können wiederum auch Unterkomponenten haben -> besser rekursiv aufrufen)
                            For Each oSubComponent In oComponent.Children
                                Debug.Print "          |--" & oSubComponent.ID
                                Debug.Print "          |  " & oSubComponent.Name
                                Debug.Print "          |  " & oSubComponent.Type
                                Debug.Print "          |  " & oSubComponent.ContainerType
                            Next
                        Next
                    Next
                Next
            Next
        End If
    End If
    
    Set oSubComponent = Nothing
    Set oComponent = Nothing
    Set oWindow = Nothing
    Set oSessionInfo = Nothing
    Set oSession = Nothing
    Set oConn = Nothing
    Set oApp = Nothing
    Set oSapGui = Nothing
End Sub

Beispiel 2 (Transaktion SE16 starten und Inhalt des ALV-Grids in Excel kopieren)

Option Explicit

Sub SAP()
    Dim oSapGui As Object
    ' Extras -> Verweise ... -> Durchsuchen ...
    ' C:\Program Files\SAP\FrontEnd\SAPgui\sapfewse.ocx
    ' Aktivieren: SAP GUI Scripting API
    Dim oApp As SAPFEWSELib.GuiApplication
    Dim oConn As SAPFEWSELib.GuiConnection
    Dim oSession As SAPFEWSELib.GuiSession
      
    ' Ref auf SAPGUI
    Set oSapGui = GetObject("SAPGUI")
    If IsObject(oSapGui) Then
        ' Ref auf ScriptingEngine
        Set oApp = oSapGui.GetScriptingEngine
        If IsObject(oApp) Then
            ' Sind Connections vorhanden?
            If oApp.Children.Count > 0 Then
                ' 1. Connection der App
                Set oConn = oApp.Children(0)
                ' 1. Session der Connection
                Set oSession = oConn.Children(0)
                
                ' Fenster minimieren
                oSession.FindById("wnd[0]").Iconify
                
                ' SE16 starten
                oSession.StartTransaction ("SE16")
                               
                ' Selektionsbild
                ' Tabelle MARA
                oSession.FindById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").Text = "MARA"
                ' Button "Tabelleninhalt (F7)"
                oSession.FindById("wnd[0]/tbar[1]/btn[7]").Press
                ' max. 10 Einträge
                oSession.FindById("wnd[0]/usr/txtMAX_SEL").Text = "10"
                ' Button "Ausführen (F8)"
                oSession.FindById("wnd[0]/tbar[1]/btn[8]").Press
                
                ' ALV-Ansicht aktivieren
                ' Menü suchen
                Dim oMenu As GuiComponent
                Set oMenu = oSession.FindById("wnd[0]/mbar")
                
                ' Menüpunkt "Einstellungen"
                Dim oOptions As GuiComponent
                Set oOptions = oMenu.FindByName("Einstellungen", "GuiMenu")
                
                ' Menüpunkt "Benutzerparameter ..."
                Dim oUserPar As GuiComponent
                Set oUserPar = oOptions.FindByName("Benutzerparameter...", "GuiMenu")
                oUserPar.Select
                
                ' Databrowser -> Ausgabeliste -> ALV-Grid-Darstellung
                Dim oALVParam As GuiComponent
                Set oALVParam = oSession.FindById("wnd[1]/usr/tabsG_TABSTRIP/tabp0400/ssubTOOLAREA:SAPLWB_CUSTOMIZING:0400/radRSEUMOD-TBALV_GRID")
                
                ' wenn ALV-Ansicht noch nicht ausgewählt, dann anhaken
                If oALVParam.Selected = vbFalse Then
                    oALVParam.Select
                End If
                
                ' Einstellungen übernehmen
                oSession.FindById("wnd[1]/tbar[0]/btn[0]").Press
                
                Set oUserPar = Nothing
                Set oOptions = Nothing
                Set oMenu = Nothing
                
                ' ALV-Grid holen
                Dim oALV As GuiComponent
                Set oALV = oSession.FindById("wnd[0]/usr/cntlGRID1/shellcont/shell")
                
                ' Zeilen
                Dim iRows As Integer
                iRows = oALV.RowCount() - 1
                ' Spalten
                Dim iCols As Integer
                iCols = oALV.ColumnCount() - 1
                
                ' Spaltentitel lesen
                Dim oColumns
                Set oColumns = oALV.ColumnOrder
                
                Dim c As Integer
                Dim r As Integer
                
                ' Überschriften einfügen
                For c = 0 To iCols
                    ActiveWorkbook.ActiveSheet.Cells(1, c + 1) = oColumns(c)
                Next
                
                ' Daten einfügen
                For r = 0 To iRows
                
                    ' ALV-Grid weiterscrollen, damit ein Update des Inhalts erfolgt
                    oALV.FirstVisibleRow = r
                    
                    For c = 0 To iCols
                        ActiveWorkbook.ActiveSheet.Cells(r + 2, c + 1) = oALV.GetCellValue(r, CStr(oColumns(c)))
                    Next
                Next
                
                ' Fenster maximieren
                oSession.FindById("wnd[0]").Maximize
            Else
                MsgBox "Bitte an einem SAP-System anmelden."
            End If
        End If
    End If
    
    Set oSession = Nothing
    Set oConn = Nothing
    Set oApp = Nothing
    Set oSapGui = Nothing
End Sub

Beispiel 3 (System-Logon)

Option Explicit

Sub SAP()
    Dim oSapGui As Object
       
    ' Extras -> Verweise ... -> Durchsuchen ...
    ' C:\Program Files\SAP\FrontEnd\SAPgui\sapfewse.ocx
    ' Aktivieren: SAP GUI Scripting API
    Dim oApp As SAPFEWSELib.GuiApplication
    Dim oConn As SAPFEWSELib.GuiConnection
    Dim oSession As SAPFEWSELib.GuiSession
      
    ' Ref auf SAPGUI
    Set oSapGui = GetObject("SAPGUI")
    If IsObject(oSapGui) Then
        ' Ref auf ScriptingEngine
        Set oApp = oSapGui.GetScriptingEngine
        If IsObject(oApp) Then

            ' Zu System verbinden, wobei <SYSTEM> == vollst. (!) Systemname aus SAP Logon Pad -> Verbindungen
            Set oConn = oApp.OpenConnection("<SYSTEM>")

            If IsObject(oConn) Then
                ' 1. Session der Connection
                Set oSession = oConn.Children(0)
  
                ' Username und Passwort setzen
                oSession.FindById("wnd[0]/usr/txtRSYST-BNAME").Text = "<USER>"
                oSession.FindById("wnd[0]/usr/pwdRSYST-BCODE").Text = "<PASS>"
                ' Enter-Taste
                oSession.FindById("wnd[0]").SendVKey 0
  
                ' SE16 starten
                oSession.StartTransaction ("SE16")
            End If
        End If
    End If
    
    Set oSession = Nothing
    Set oConn = Nothing
    Set oApp = Nothing
    Set oSapGui = Nothing
End Sub

Links

SAP-News

Horst Keller

  • ABAP

Rich Heilman

  • SAP HANA

Thomas Fiedler

  • ABAP

Frank Buchholz

  • Architekt SAP CoE

Thomas Jung

  • SAP HANA

Andre Fischer

  • SAP Fiori, SAP Gateway, Odata

Uwe Fetzer

  • ABAP

Joachim Rees

  • S/4HANA, ABAP

Wolfgang Woehrle

  • ABAP, ADT

Enno Wulff

  • ABAP

Olga Dolinskaja

  • ABAP, ATC

Bartosz Jarkowski

  • HANA

Mauricio Lauffer

  • SAPUI5, OpenUI5

Lars Hvam

S/4HANA Blog

  • Infos zum Thema S/4HANA

S/4HANA-Experts

  • Infos zum Thema S/4HANA

[SAPUI5] Übersicht

OpenUI5

OpenUI5

  • Einstiegsseite zum Thema OpenUI5 von SAP

OpenUI5 auf GitHub

  • OpenUI5 auf GitHub

OpenUI5: UI Development Toolkit for HTML5

  • API Reference, Dev Guide, UI5 Explorer, Demos, Icon Explorer, Control Library

OpenUI5 Versions Maintenance Status

  • Versionsübersicht

SAPUI5

csharp-blog.de

  • Blog zum Thema SAPUI5

SAPUI5: UI Development Toolkit for HTML5

  • API Reference, Dev Guide, UI5 Explorer, Demos, Icon Explorer, Control Library

Bootstrapping: Loading and Initializing

  • Tutorial zum Thema Boostrapping

SAPUI5 Versions Maintenance Status

  • Versionsübersicht

UI5 Demo Kit

  • Dokumentation

UI5 Programming Examples

  • Programmierbeispiele für UI5

SAPUI5: Ein paar Grundlagen zum SAPUI5-Framework

  • Grundlagen

SAPUI5: Typen von Views

  • Views

SAPUI5: Data Binding

  • Data Binding

[SAP Gateway] OData Service Implementierung

OData service development with SAP Gateway

  • Teil 1

OData service development with SAP Gateway – Part II

  • Teil 2

OData service with annotations

  • Beispiel

SAP ODATA Grundlagen: ODATA-Services anlegen und testen

  • Beispiel

CRUDQ and Function Import operations in OData service

  • CRUDQ

Association / Navigation and data provider expand in OData service

  • Association / Navigation

A Step by Step process to create Odata services in SAP / SAP HANA system

  • OData-Service einrichten und implementieren

Building OData Services [1] : OData Overview

  • OData-Service einrichten und implementieren

Building OData Services [2] : Understanding OData Service

  • OData-Service einrichten und implementieren

Building OData Services [3] : Create OData Service

  • OData-Service einrichten und implementieren

Building OData Services [4] : Register and Test OData Service

  • OData-Service einrichten und implementieren

Building OData Services [5] : Working With OData URIs

  • OData-Service einrichten und implementieren

Building OData Services [6] : Implementing all URIs / CRUD Operations

  • OData-Service einrichten und implementieren

Building OData Services [1] – [25]

  • OData-Service einrichten und implementieren

OData Service based on RFCs – Part 1

  • Teil 1

OData Service based on RFCs – Part 2

  • Teil 2

OData Service based on RFCs – Part 3

  • Teil 3

Introduction to OData and how to implement them in ABAP

  • Übersicht zu OData und Implementierungsbeispiele

ABAP Test Double Framework

  • ABAP Test Double Framework

Bring Data to Life – Integrating D3.js in SAP via RESTful Web Service – Part 1

  • SAP: REST-Service mit HTML + JavaScript implementieren

Bring Data to Life – Integrating D3.js in SAP via RESTful Web Service – Part 2

  • SAP: REST-Service mit HTML + JavaScript implementieren

Bring Data to Life – Integrating D3.js in SAP via RESTful Web Service – Part 3

  • SAP: REST-Service mit HTML + JavaScript implementieren