[MS Excel] Tabellendaten per RFC-Zugriff (RFC_READ_TABLE) aus einem SAP-System importieren

Sub RFCReadTable()
    ' https://www.linkedin.com/pulse/connect-sap-r3-call-custom-fm-from-ms-excel-erkan-kopuz
    ' https://saplsmw.com/Import_tables_directly_into_Access_from_SAP_using_RFCs
    ' http://sapass.metro.client.jp/Sap_Active_X/UseFunctionControl.htm
    Set oSAP = CreateObject("SAP.Functions")
    oSAP.Connection.ApplicationServer = "1.1.1.1" ' IP des Appl-Servers (SM51->Details)
    oSAP.Connection.SystemNumber = "01"           ' Systemnummer, meißt im Namen des Appl-Servers enthalten
    oSAP.Connection.System = "XA1"                ' Entwicklungs-, Test-, Produktivsystem
    oSAP.Connection.Client = "100"                ' Mandant
    oSAP.Connection.Language = "DE"               ' Sprache "EN", "DE" ...
    oSAP.Connection.User = "USER1"                ' SAP-User
    'oSAP.Connection.Password = "xyz"              ' SAP-Passwort
    oSAP.Connection.UseSAPLogonIni = False
    
    ' RFC-Login, wobei
    ' Logon(0, False): Logon-Fenster anzeigen
    ' Logon(0, True): Silent logon, Passwort muss gesetzt sein
    If oSAP.Connection.Logon(0, False) = True Then
      Dim oFuBa As Object
        
      ' FuBa RFC_READ_TABLE abfragen
      Set oFuBa = oSAP.Add("RFC_READ_TABLE")
      
      ' EXPORTING
      Set e_query_table = oFuBa.Exports("QUERY_TABLE")
      Set e_delimiter = oFuBa.Exports("DELIMITER")
      Set e_rowCount = oFuBa.Exports("ROWCOUNT")
      
      e_query_table.Value = "STXH" ' Tabelle STXH
      e_delimiter.Value = ";"      ' Spalten mit ";" getrennt
      e_rowCount.Value = "100"     ' max. 100 Datensätze lesen, 0 = alle
      
      ' TABLES
      Set t_options = oFuBa.Tables("OPTIONS")
      Set t_fields = oFuBa.Tables("FIELDS")
      Set t_data = oFuBa.Tables("DATA")
      
      ' WHERE-Bedingung
      t_options.AppendRow
      t_options(1, "TEXT") = "TDOBJECT EQ 'TEXT'"
      
      ' Welche Spalten sollen gelesen werden
      t_fields.AppendRow
      t_fields(1, "FIELDNAME") = "TDOBJECT"
      t_fields.AppendRow
      t_fields(2, "FIELDNAME") = "TDNAME"
      t_fields.AppendRow
      t_fields(3, "FIELDNAME") = "TDID"
      t_fields.AppendRow
      t_fields(4, "FIELDNAME") = "TDTITLE"
      t_fields.AppendRow
      t_fields(5, "FIELDNAME") = "TDLUSER"
      
      If oFuBa.Call = True Then
        ' Schnittstellenparameter "TABLES-DATA" holen
        ' Rückgabe in Excel-Sheet einfügen
        
        Dim iRow As Integer
        iRow = 1
            
        ' Rückgabemenge beinhaltet Strings, die mit dem festgelegten Separator ";" getrennt sind
        For Each oDataLine In t_data.Rows

          ' Spalten einer Datenzeile anhand des Separators ";" aufsplitten
          Dim vFields As Variant
          vFields = Split(oDataLine(1), ";")
          
          ' Inhalt der Spalten in die Zellen schreiben
          For iCol = LBound(vFields) To UBound(vFields)
            ActiveWorkbook.Sheets(1).Cells(iRow, iCol + 1) = Trim(vFields(iCol))
          Next iCol
                
         iRow = iRow + 1
        Next
      Else
        ' Exception?
        MsgBox oFuBa.Exception
      End If

      ' Logoff
      oSAP.Connection.Logoff
    
    Else
      ' Kein Login möglich
      MsgBox "Login fehlgeschlagen."
    End If

End Sub

[MS Excel] Nutzerliste per RFC-Zugriff (TH_USER_LIST) aus einem SAP-System importieren

Sub GetUserList()
  ' https://www.linkedin.com/pulse/connect-sap-r3-call-custom-fm-from-ms-excel-erkan-kopuz
  ' https://saplsmw.com/Import_tables_directly_into_Access_from_SAP_using_RFCs
  ' http://sapass.metro.client.jp/Sap_Active_X/UseFunctionControl.htm

  ' SAP-Objekt erzeugen
  Set oSAP = CreateObject("SAP.Functions")
  oSAP.Connection.ApplicationServer = "1.1.1.1"     ' IP des Appl-Servers (SM51->Details)
  oSAP.Connection.SystemNumber = "01"               ' Systemnummer, meißt im Namen des Appl-Servers enthalten
  oSAP.Connection.System = "DV1"                    ' Entwicklungs-, Test-, Produktivsystem
  oSAP.Connection.Client = "100"                    ' Mandant
  oSAP.Connection.Language = "DE"                   ' Sprache "EN", "DE" ...
  oSAP.Connection.User = "USER1"                    ' SAP-User
  'oSAP.Connection.Password = "xyz"                  ' SAP-Passwort
  oSAP.Connection.UseSAPLogonIni = False
    
  ' RFC-Login, wobei
  ' Logon(0, False): Logon-Fenster anzeigen
  ' Logon(0, True): Silent logon, Passwort muss gesetzt sein
  If oSAP.Connection.Logon(0, False) = True Then
    Dim oFuBa As Object
        
    ' FuBa TH_USER_LIST abfragen
    Set oFuBa = oSAP.Add("TH_USER_LIST")

    If oFuBa.Call = True Then
        
      ' Schnittstellenparameter "TABLES-USRLIST"
      Dim oUsrList As Object
      Set oUsrList = oFuBa.Tables("USRLIST")
             
      ' Userliste in Excel-Sheet einfügen         
      Dim i As Integer
      i = 1
            
      For Each User In oUsrList.Rows
               
        ActiveWorkbook.Sheets(1).Cells(i, 1) = User(2)  ' Client
        ActiveWorkbook.Sheets(1).Cells(i, 2) = User(3)  ' UserName
        ActiveWorkbook.Sheets(1).Cells(i, 3) = User(5)  ' Terminal
        ActiveWorkbook.Sheets(1).Cells(i, 4) = User(16) ' IP
               
        i = i + 1
      Next
    Else
      ' Exception?
      MsgBox oFuBa.Exception
    End If

    ' Logoff
    oSAP.Connection.Logoff
    
  Else
    ' Kein Login möglich
    MsgBox "Login fehlgeschlagen."
  End If

End Sub

[ABAP] System-Funktion des c-kernel aufrufen

Vorsicht: Unsachgemäßer Aufruf von Kernelfunktionen kann Systemschäden verursachen!

* Server-Directory auflisten
DATA: lv_cmd TYPE char255.
DATA: lt_result_tab TYPE TABLE OF char255.

lv_cmd = 'ls -lasi'.

CALL 'SYSTEM' ID 'COMMAND' FIELD lv_cmd ID 'TAB' FIELD lt_result_tab.

LOOP AT lt_result_tab INTO DATA(res_line).
  WRITE: / res_line.
ENDLOOP.

* aufrufende Transaktion
DATA: lv_trans TYPE tcode.
CALL 'GET_PARAM_TCOD' ID 'PTCOD' FIELD lv_trans.
WRITE: / lv_trans.

* eingegebenen OK-Code ermitteln
DATA: my_ucomm TYPE sy-ucomm.
CALL 'DYNP_OKCODE_GET' ID 'FCODE' FIELD my_ucomm.
WRITE: / my_ucomm.

* CPUID ermitteln
DATA: local_host TYPE spfid-host.
CALL 'C_GET_CPU_ID' ID 'CPUID' FIELD local_host.
WRITE: / local_host.

* SAP-System ermitteln
DATA: lv_sapsystem TYPE spfid-sysnr.
CALL 'C_GET_SYSTEM_NUMBER' ID 'SYSTEM' FIELD lv_sapsystem.
WRITE: / lv_sapsystem.

* SAP Servername
DATA: lv_server_name LIKE btctgtsrvr-srvname.
CALL 'C_SAPGPARAM' ID 'NAME' FIELD 'rdisp/myname' ID 'VALUE' FIELD lv_server_name.
WRITE: / lv_server_name.

* AL11 DIR_HOME
DATA: lv_dir_home TYPE dirname_al11.
CALL 'C_SAPGPARAM' ID 'NAME' FIELD 'DIR_HOME' ID 'VALUE' FIELD lv_dir_home.
WRITE: / lv_dir_home.

* AL11 DIR_TRANS
DATA: lv_dir_trans TYPE dirname_al11.
CALL 'C_SAPGPARAM' ID 'NAME' FIELD 'DIR_TRANS' ID 'VALUE' FIELD lv_dir_trans.
WRITE: / lv_dir_trans.

* Namens-Format für Audit-Files
DATA: lv_aufn TYPE rsauflist-file_name.
CALL 'C_SAPGPARAM' ID 'NAME' FIELD 'FN_AUDIT' ID 'VALUE' FIELD lv_aufn.
WRITE: / lv_aufn.

* Directory für Audit-Files
DATA: lv_audit TYPE dirname_al11.
CALL 'C_SAPGPARAM' ID 'NAME' FIELD 'DIR_AUDIT' ID 'VALUE' FIELD lv_audit.
WRITE: / lv_audit.

Links