AW: VBA - SAP Daten auslesen
22.07.2021 14:03:34
ede
Hallo MarC,
hab das mal vor vielen Jahren für eine SAP-Tabelle gemacht, anbei der CODE, wobei du die sapConnection. auf dein SAP-System anpassen musst:
Sub SAP_read_table()
Dim functionCtrl As Object
Dim sapConnection As Object
Dim FUBAU_rfc_read_table As Object
Dim T_I_Options As Object
Dim T_I_Fields As Object
Dim T_E_Data As Object
Dim i, x As Integer
Dim strDataRow As String
Dim DataRow As Variant
Dim Col As Boolean
Col = False
Set functionCtrl = CreateObject("SAP.Functions")
Set sapConnection = functionCtrl.Connection
sapConnection.Client = "999"
sapConnection.User = "DEIN_SAP-USERNAME"
sapConnection.Password = InputBox("Bitte Kennwort eingeben") '
sapConnection.Language = "DE"
sapConnection.SystemNumber = "20"
sapConnection.System = "DEIN_SYSTEM"
sapConnection.ApplicationServer = "DEIN_SAP_SERVER"
If sapConnection.Logon(1, True) True Then
MsgBox "Keine Verbindung zum SAP!"
Exit Sub
End If
Set FUBAU_rfc_read_table = functionCtrl.Add("RFC_READ_TABLE")
With FUBAU_rfc_read_table
.exports("QUERY_TABLE") = InputBox("Bitte Tabellenname eingeben")
.exports("DELIMITER") = "|" 'Delimiter
'.exports("ROWCOUNT") = 100 ' Anzahl an Datensätze
End With
Set T_I_Options = FUBAU_rfc_read_table.tables("OPTIONS")
Set T_I_Fields = FUBAU_rfc_read_table.tables("FIELDS")
Set T_E_Data = FUBAU_rfc_read_table.tables("DATA")
'Aufruf des FUBAs
ret = FUBAU_rfc_read_table.call
'Übetragen der Daten in Excel-Tabelle (z.B. immer in Tabelle1)
If T_E_Data.RowCount > 0 And ret = True Then
For i = 1 To T_E_Data.RowCount
strDataRow = T_E_Data(i, 1)
DataRow = Split(strDataRow, "|")
'Spaltenüberschriften an Excel übergeben
If Col = False Then
For x = 0 To UBound(DataRow)
Tabelle1.Cells(1, x + 1).Value = T_I_Fields(x + 1, 1)
Next x
Col = True
End If
For x = 0 To UBound(DataRow)
Tabelle1.Cells(i + 1, x + 1).Value = DataRow(x)
Next x
Next i
End If
End Sub
gruss
ede