VBA & RFC_READ_TABLE mit GUI750 SP8


Hinweise, Tips und Tricks, FAQs - keine Anfragen!!

Moderatoren: Jan, Steff, SRMler

VBA & RFC_READ_TABLE mit GUI750 SP8

Beitragvon Tron » 10.11.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
<:: 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.

Für diese Nachricht hat Tron 2 Dankeschön bekommen :
ewx, Thomas R.
Tron
Expert
 
Beiträge: 954
Registriert: 04.08.2007, 21:08
Wohnort: Hamburg
Dank erhalten: 173 mal
Ich bin: Entwickler/in

Sponsor

Alte ABAP-Entwicklerweisheit: Weißt du weder aus noch ein, baust du einen BADI ein

Zurück zu Tips + Tricks & FAQs

  Aktuelle Beiträge   
s4hana-cloud
vor 8 Stunden von sap_inchen 1 Antw.
500 Internal Server Error
vor 16 Stunden von zzcpak 1 Antw.
Dokumentinformationen lesen vom DVS
vor 18 Stunden von Tron 4 Antw.
Tabs innerhalb von Tabs
vor 19 Stunden von ewx 4 Antw.
Fakturierungsplan in Kontrakten ändern
vor 15 Stunden von DeathAndPain 1 Antw.

  Ähnliche Beiträge beta
Keine Beiträge gefunden - versuche es mit der erweiterten Suche.

 

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder