[MS Excel] BAPI-Zugriff aus Excel (VBA) heraus

Option Explicit

Sub GetMATNR()
    ' Verbindung zu SAP herstellen
    Dim oSAP As Object
    Set oSAP = CreateObject("SAP.Functions")
    ' Verbindungsdaten vorbelegen
    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 = "XD1"                ' Entwicklungs-, Test-, Produktivsystem
    oSAP.Connection.Client = "100"                ' Mandant
    oSAP.Connection.Language = "DE"               ' Sprache "EN", "DE" ...
    
    ' RFC-Login: Logon-Fenster anzeigen
    If oSAP.Connection.Logon(0, False) = True Then
        ' BAPI_MATERIAL_GET_DETAIL abfragen
        Dim oFuBa As Object
        Set oFuBa = oSAP.Add("BAPI_MATERIAL_GET_DETAIL")
        
        ' EXPORTING
        Dim e_material As Variant
        Set e_material = oFuBa.Exports("MATERIAL")
        e_material.Value = ActiveWorkbook.ActiveSheet.Cells(1, 2) ' MATNR in B1
        
        ' IMPORTING
        Dim i_material_general_data As Variant
        Set i_material_general_data = oFuBa.Imports("MATERIAL_GENERAL_DATA")
        
        Dim i_return As Variant
        Set i_return = oFuBa.Imports("RETURN")
        
        ' Wenn Fuba-Aufruf ok
        If oFuBa.Call = True Then
            ' RETURN auswerten
            If i_return.Value("TYPE") = "E" Then
                ' Bei Fehler: Fehlermessage ausgeben in B2
                ActiveWorkbook.ActiveSheet.Cells(2, 2) = i_return.Value("MESSAGE")
            Else
                ' Wenn OK: Materialkurztext in B2
                ActiveWorkbook.ActiveSheet.Cells(2, 2) = i_material_general_data.Value("MATL_DESC")
            End If
        End If
    End If
End Sub

[ABAP] Daten zu einem Material (MATNR) ermitteln

DATA: lv_material_general_data TYPE bapimatdoa.
DATA: lv_return	TYPE bapireturn.
DATA: lv_materialplantdata TYPE bapimatdoc.
DATA: lv_materialvaluationdata TYPE bapimatdobew.

* Materialdaten holen
CALL FUNCTION 'BAPI_MATERIAL_GET_DETAIL'
  EXPORTING
    material              = '000000001234567890'   " MATNR
    valuationarea         = '01'                   " Werk
  IMPORTING
    material_general_data = lv_material_general_data
    return                = lv_return
    materialplantdata     = lv_materialplantdata
    materialvaluationdata = lv_materialvaluationdata.

* Fehlermessage
WRITE: / lv_return-type, lv_return-code, lv_return-message.

* Kurzbezeichnung
WRITE: / lv_material_general_data-matl_desc.

* Preis
WRITE: / SWITCH string( lv_materialvaluationdata-price_ctrl
                        WHEN 'S' THEN lv_materialvaluationdata-std_price
                        ELSE lv_materialvaluationdata-moving_pr ), lv_materialvaluationdata-currency.