VBA & RFC_READ_TABLE mit GUI750 SP8

Hinweise, Tips und Tricks, FAQs - keine Anfragen!!
1 Beitrag / Seite 1 von 1
1 Beitrag Seite 1 von 1

VBA & RFC_READ_TABLE mit GUI750 SP8

Beitrag von Tron (Expert / 992 / 8 / 196 ) » 10. Nov 2018 18:15

Hallo.
Falls jemand weiterhin Remote mit VBA auf das SAP System zugreifen möchte, habe ich nach langem probieren eine Lösung ausgearbeitet.
1.) Verweise
VBARef.png
1.) SAP Logon Unicode Control
2.) SAP Remote Function Call Unicode Control
3.) SAP Table Factory Unicode
Befinden sich alle im Verzeichnis C:\Program Files (x86)\SAP\FrontEnd\SAPgui\Unicode\*.ocx

2.) Das VBA Module
VBARef2.txt
' Example: calling BAPI RFC_READ_TABELE
Option Explicit
Public Functions As SAPFunctionsOCX.SAPFunctions
Public TableFactory As SAPTableFactoryCtrl.SAPTableFactory

Private LogonControl As SAPLogonCtrl.SAPLogonControl
Private Connection As SAPLogonCtrl.Connection

Dim Func As SAPFunctionsOCX.Function
'Dim Bapi As SAPBAPIControlLib.SAPBusinessObject

Public TabObj As Object
Private Sub Logon()
Dim retcode As Boolean
Dim SilentLogon As Boolean

' Get SAP Objects
Set LogonControl = CreateObject("SAP.LogonControl.Unicode.1")
Set Functions = CreateObject("SAP.Functions.Unicode")
Set TableFactory = CreateObject("SAP.TableFactory.Unicode.1")

'Define Connection objetc
Set Connection = LogonControl.NewConnection

Connection.UseSAPLogonIni = True
SilentLogon = False 'True, if you want to provide Logon parameters

retcode = Connection.Logon(1, SilentLogon)

'Check return code
If retcode <> True Then
MsgBox "Logon failed"
Exit Sub
End If

'Assign connection
Functions.Connection = Connection

End Sub



Private Sub Main()
Dim ix As Long
Dim imax As Long

Dim T() As String
Dim i As Long
Dim k As Long


'Assign SAP rfc function module
Set Func = Functions.Add("RFC_READ_TABLE")
'Func.Exports("DELIMITER") = vbTab


'Import Paramteres
Dim iQuery_Table As SAPFunctionsOCX.Parameter
Dim iDelimiter As SAPFunctionsOCX.Parameter
Dim iNo_Data As SAPFunctionsOCX.Parameter
Dim iRowSkips As SAPFunctionsOCX.Parameter
Dim iRowCounts As SAPFunctionsOCX.Parameter

'Export

'Tables
Dim tOptions As SAPTableFactoryCtrl.Table
Dim tFields As SAPTableFactoryCtrl.Table
Dim tData As SAPTableFactoryCtrl.Table


'Import Parameter of rfc function module
Set iQuery_Table = Func.Exports("QUERY_TABLE")
Set iDelimiter = Func.Exports("DELIMITER")
Set iNo_Data = Func.Exports("NO_DATA")
Set iRowSkips = Func.Exports("ROWSKIPS")
Set iRowCounts = Func.Exports("ROWCOUNT")

iDelimiter.Value = vbTab

'Export Parameter of rfc function module
'not used by rfc_read_table

'Tables rfc function module
Set tFields = Func.Tables("FIELDS")
Set tOptions = Func.Tables("OPTIONS")
Set tData = Func.Tables("DATA")

'Assign values to Import parameter
iQuery_Table.Value = "T001W"

'Assign values to Import tables
tOptions.AppendRow
tOptions(1, "TEXT") = "WERKS = '8000'" 'max 72 char

tFields.AppendRow
tFields(1, "FIELDNAME") = "WERKS"

tFields.AppendRow
tFields(2, "FIELDNAME") = "NAME1"

Func.Call
Debug.Print tData.RowCount
imax = tData.RowCount
For ix = 1 To imax
Debug.Print tData(ix, 1)
i = 0
T = Split(tData(ix, 1), vbTab)
Do While i < tFields.RowCount
Debug.Print T(i)
i = i + 1
Loop
Next

'Release rfc function
Functions.RemoveAll

End Sub


Private Sub Main2()
Dim ix As Long
Dim imax As Long

Dim T() As String
Dim i As Long
Dim k As Long

'Assign SAP rfc function module
Set Func = Functions.Add("RFC_READ_TABLE")

'Func.Exports("DELIMITER") = vbTab

'Import Paramteres
Dim iQuery_Table As SAPFunctionsOCX.Parameter
Dim iDelimiter As SAPFunctionsOCX.Parameter
Dim iNo_Data As SAPFunctionsOCX.Parameter
Dim iRowSkips As SAPFunctionsOCX.Parameter
Dim iRowCounts As SAPFunctionsOCX.Parameter

'Export

'Tables
Dim tOptions As SAPTableFactoryCtrl.Table
Dim tFields As SAPTableFactoryCtrl.Table
Dim tData As SAPTableFactoryCtrl.Table


'Import Parameter of rfc function module
Set iQuery_Table = Func.Exports("QUERY_TABLE")
Set iDelimiter = Func.Exports("DELIMITER")
Set iNo_Data = Func.Exports("NO_DATA")
Set iRowSkips = Func.Exports("ROWSKIPS")
Set iRowCounts = Func.Exports("ROWCOUNT")

iDelimiter.Value = vbTab


'Export Parameter of rfc function module
'not used by rfc_read_table

'Tables rfc function module
Set tFields = Func.Tables("FIELDS")
Set tOptions = Func.Tables("OPTIONS")
Set tData = Func.Tables("DATA")

'Assign values to Import parameter
iQuery_Table.Value = "MSEG"

'Assign values to Import tables
tOptions.AppendRow
tOptions(1, "TEXT") = "MBLNR EQ '4900589198' AND MJAHR EQ '2018' AND BWART EQ '412'" 'max 72 char

tFields.AppendRow
tFields(1, "FIELDNAME") = "MBLNR"
tFields.AppendRow
tFields(2, "FIELDNAME") = "MJAHR"
tFields.AppendRow
tFields(3, "FIELDNAME") = "ZEILE"
tFields.AppendRow
tFields(4, "FIELDNAME") = "BWART"
tFields.AppendRow
tFields(5, "FIELDNAME") = "MATNR"
tFields.AppendRow
tFields(6, "FIELDNAME") = "WERKS"
tFields.AppendRow
tFields(7, "FIELDNAME") = "LGORT"
tFields.AppendRow
tFields(8, "FIELDNAME") = "CHARG"
tFields.AppendRow
tFields(9, "FIELDNAME") = "SOBKZ"
tFields.AppendRow
tFields(10, "FIELDNAME") = "LIFNR"
tFields.AppendRow
tFields(11, "FIELDNAME") = "KUNNR"
tFields.AppendRow
tFields(12, "FIELDNAME") = "KDAUF"
tFields.AppendRow
tFields(13, "FIELDNAME") = "KDPOS"
tFields.AppendRow
tFields(14, "FIELDNAME") = "PLPLA"
tFields.AppendRow
tFields(15, "FIELDNAME") = "ERFMG"
tFields.AppendRow
tFields(16, "FIELDNAME") = "ERFME"
tFields.AppendRow
tFields(17, "FIELDNAME") = "EBELN"
tFields.AppendRow
tFields(18, "FIELDNAME") = "EBELP"
tFields.AppendRow
tFields(19, "FIELDNAME") = "SGTXT"
tFields.AppendRow
tFields(20, "FIELDNAME") = "WEMPF"
tFields.AppendRow
tFields(21, "FIELDNAME") = "ABLAD"
tFields.AppendRow
tFields(22, "FIELDNAME") = "KOSTL"
tFields.AppendRow
tFields(23, "FIELDNAME") = "AUFNR"
tFields.AppendRow
tFields(24, "FIELDNAME") = "ANLN1"
tFields.AppendRow
tFields(25, "FIELDNAME") = "ANLN2"
tFields.AppendRow
tFields(26, "FIELDNAME") = "GJAHR"
tFields.AppendRow
tFields(27, "FIELDNAME") = "PS_PSP_PNR"
tFields.AppendRow
tFields(28, "FIELDNAME") = "BSTMG"
tFields.AppendRow
tFields(29, "FIELDNAME") = "BSTME"
tFields.AppendRow
tFields(30, "FIELDNAME") = "MANDT"

Func.Call
Debug.Print tData.RowCount
imax = tData.RowCount
For ix = 1 To imax
Debug.Print tData(ix, 1)
i = 0
T = Split(tData(ix, 1), vbTab)
Do While i < tFields.RowCount
Debug.Print T(i)
i = i + 1
Loop
Next

'Release rfc function
Functions.RemoveAll

End Sub

Sub Start()
Logon
Main
Main2
Main
Main2
Main
Main2
Connection.Logoff
End Sub
ein bischen tricky war die Trennung von Logon und dem Function Call bzw. mehrer Aufrufe von Tabellen.

1.) Nachtrag: Kollegen haben erfolgreich mit SAPGUI 7.5 SP7 getestet.
2.) Nachtrag: Tipfehler -> Nicht Set iDelimeter = Func.Exports("DELIMETER") , sondern Set iDelimiter = Func.Exports("DELIMITER")
dann klappts auch mit iDelimiter.value = vbtab !


gruß Jens

Folgende Benutzer bedankten sich beim Autor Tron für den Beitrag (Insgesamt 3):
ewxThomas R.black_adept

<:: XING-Gruppe Tricktresor::>
Die deutsche Rechtschreibung ist Freeware, du darfst sie kostenlos nutzen –
Aber sie ist nicht Open Source, d. h. du darfst sie nicht verändern oder in veränderter Form veröffentlichen.


Seite 1 von 1

Aktuelle Forenbeiträge

Business Partner Konzept
vor 2 Stunden von msfox 3 / 217
Exception Handling FuBa test
vor 9 Stunden von ichse18577 1 / 40
CDS-Views / AMDP für HCM
vor 12 Stunden von RaCDigger 6 / 316
Kreditlimitprüfung Obligo
vor 16 Stunden von SAP_ENTWICKLER 3 / 163

Unbeantwortete Forenbeiträge

Exception Handling FuBa test
vor 9 Stunden von ichse18577 1 / 40
Verursachervormerkung OCM manuell anlegen
vor 6 Tagen von Aba 1 / 127
Auflösen MILL_OC - Auftragszusammenfassung
vor einer Woche von tofralu 1 / 109
Löschen von archivierten Drucklisten
vor einer Woche von Asaph 1 / 99