' Lesson 4: VBA module. Import into Excel (Alt+F11 > File > Import). Reads the table name from Sheet1!B1.
Option Explicit
Function GetSession() As Object
    Dim sapGui As Object, app As Object
    On Error Resume Next
    Set sapGui = GetObject("SAPGUI")
    If sapGui Is Nothing Then MsgBox "Open SAP and log in first.": Exit Function
    Set app = sapGui.GetScriptingEngine
    Set GetSession = app.Children(0).Children(0)
End Function
Sub RunSE16N()
    Dim session As Object
    Set session = GetSession()
    If session Is Nothing Then Exit Sub
    session.findById("wnd[0]/tbar[0]/okcd").Text = "/nSE16N"
    session.findById("wnd[0]").sendVKey 0
    session.findById("wnd[0]/usr/ctxtGD-TAB").Text = ThisWorkbook.Sheets(1).Range("B1").Value
    session.findById("wnd[0]").sendVKey 0
    session.findById("wnd[0]/tbar[1]/btn[8]").press
    MsgBox "Report started in SAP."
End Sub
