VBA und SAP: Funktionsbausteine aufrufen

Aufgabenstellung

Die Aufgabenstellung sowie der Ansatz ist identisch mit diesem Artikel (Bitte zuerst lesen).

Prämissen / Vorraussetzungen

Die Datei librfc32.dll muss im VBA Editor eingebunden werden (sie ist nach der Installation der SAPGUI verfügbar).

Lösung

Aufbau der Verbindung

Public Function SAP_Logon(username As String, password As String) As Boolean
'*************************************************
'  ANMELDUNG AN SAP
'  ACHTUNG: USER MUSS BERECHTIGUNGEN HABEN!!!!
'*************************************************

    Set FunctionCtrl = CreateObject("SAP.Functions")
    'Objekt für die SAP Verbindung

    Set SapConnection = FunctionCtrl.Connection
   
    SapConnection.Client = "100"
    SapConnection.User = username
    SapConnection.Language = "DE"
    SapConnection.password = password
    
     SapConnection.hostname = "rechnername.firma.com" 'nicht das kuerzel wie DE9
    
    SapConnection.systemnumber = "0"

    If Not SapConnection.Logon(0, True) Then 'True silent - false offen
        MsgBox "Logon failed!!!", vbCritical
        CMS_Logon = False
    Else
        CMS_Logon = True
    End If
End Function

Abmelden

Public Function SAP_Logoff() As Boolean
    SapConnection.LogOff
End Function

Funktionbaustein befüllen und aufrufen

Public Function SAP_Create_Request() As Long
'******************************************************
'  Request erzeugen
'  Strukturen füllen und übergeben, dann Log ausgeben
'******************************************************

On Error GoTo ErrorMSG

    Dim FunctionModule As Object
    Dim e_EXPORTSTRUKTUR As Object
    Dim T_TABELLENSTRUKTUR As Object
    
    Dim te_messtab As Object
    
    Dim lCnt As Long

'   Assign Function Module
    Set FunctionModule = FunctionCtrl.Add("Z_FUBA")
    
'   Set export Variables
    Set e_EXPORTSTRUKTUR = FunctionModule.Exports("P_STRUKTURPARAMETER")

' Set structrue fields in export parameter
    e_EXPORTSTRUKTUR ("ZEXPORTPARAM1") = "BLA"
    e_EXPORTSTRUKTUR ("ZEXPORTPARAM1") = "BLA2"

    Set T_TABELLENSTRUKTUR  = FunctionModule.Tables("T_ZMMMATANF8")
    T_TABELLENSTRUKTUR.appendRow
    T_TABELLENSTRUKTUR(1, "SPALTE1") = 1
    T_TABELLENSTRUKTUR(1, "SPALTE2") = 2

    T_TABELLENSTRUKTUR.appendRow
    T_TABELLENSTRUKTUR(2, "SPALTE1") = 3
    T_TABELLENSTRUKTUR(2, "SPALTE2") = 4

'   Call Function Aufruf
    If FunctionModule.Call = True Then
        Set te_messtab = FunctionModule.Tables("TE_MESSTAB")
        ' Meldungen ausgeben:
        
        Dim intRow As Integer
        For intRow = 1 To te_messtab.RowCount
            If te_messtab(intRow, "ARBGB") = "ZMM0001" And _
                te_messtab(intRow, "MSGNR") = "003" Then

                ' ANForderung wurde erstellt.
                CMS_Create_Request = Val(te_messtab(intRow, "MSGV1"))
            End If
            Debug.Print te_messtab(intRow, "ARBGB")
            Debug.Print te_messtab(intRow, "MSGNR")
            Debug.Print te_messtab(intRow, "NATXT_DE")
            sMSGTXT = te_messtab(intRow, "NATXT_DE")
            Debug.Print te_messtab(intRow, "FLDNAME")
            Debug.Print te_messtab(intRow, "MSGV1")
            Debug.Print te_messtab(intRow, "MSGV2")
            Debug.Print te_messtab(intRow, "MSGV3")
            Debug.Print te_messtab(intRow, "MSGV4")
            Debug.Print "----------------------------------"
        Next
    Else
        CMS_Create_Request = 0
         MsgBox "Error creating the CMS Request." & vbNewLine & _
                "See Log for details", vbCritical
    End If
ErrorMSG:
  
End Function

Die Testfunktion

Public Sub Start()
'*************************************************
'  TESTUMGEBUNG
'*************************************************
    Dim RequestNo As Long
    Dim username As String
    Dim password As String
    
    username = "BJOERN"
    password = "ICHBINDERBESTE"
        
    Call CMS_Logon(username, password)
    RequestNo = SAP_Create_Request   
    Call CMS_Logoff
End Sub

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.