VBA & RFC_READ_TABLE mit GUI750 SP8

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

VBA & RFC_READ_TABLE mit GUI750 SP8

Postby Tron » Fri Feb 01, 2019 8:30 am

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
VBARef.png (23.65 KiB) Viewed 23716 times

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


Code: [Select all] [Expand/Collapse] [Download] (Untitled.txt)
  1. ' Example: calling BAPI RFC_READ_TABELE
  2. Option Explicit
  3. Public Functions As SAPFunctionsOCX.SAPFunctions
  4. Public TableFactory As SAPTableFactoryCtrl.SAPTableFactory
  5.  
  6. Private LogonControl As SAPLogonCtrl.SAPLogonControl
  7. Private Connection As SAPLogonCtrl.Connection
  8.  
  9. Dim Func As SAPFunctionsOCX.Function
  10. 'Dim Bapi As SAPBAPIControlLib.SAPBusinessObject
  11.  
  12. Public TabObj As Object
  13. Private Sub Logon()
  14.     Dim retcode As Boolean
  15.     Dim SilentLogon As Boolean
  16.    
  17. ' Get SAP Objects
  18.     Set LogonControl = CreateObject("SAP.LogonControl.Unicode.1")
  19.     Set Functions = CreateObject("SAP.Functions.Unicode")
  20.     Set TableFactory = CreateObject("SAP.TableFactory.Unicode.1")
  21.    
  22.  'Define Connection objetc
  23.     Set Connection = LogonControl.NewConnection
  24.    
  25.     Connection.UseSAPLogonIni = True
  26.     SilentLogon = False  'True, if you want to provide Logon parameters
  27.    
  28.     retcode = Connection.Logon(1, SilentLogon)
  29.    
  30.  'Check return code
  31.     If retcode <> True Then
  32.         MsgBox "Logon failed"
  33.         Exit Sub
  34.     End If
  35.    
  36.  'Assign connection
  37.     Functions.Connection = Connection
  38.  
  39. End Sub
  40.  
  41.  
  42.  
  43. Private Sub Main()
  44.     Dim ix As Long
  45.     Dim imax As Long
  46.    
  47.     Dim T() As String
  48.     Dim i As Long
  49.     Dim k As Long
  50.  
  51.    
  52.  'Assign SAP rfc function module
  53.  Set Func = Functions.Add("RFC_READ_TABLE")
  54.  'Func.Exports("DELIMITER") = vbTab
  55.    
  56.    
  57. 'Import Paramteres
  58. Dim iQuery_Table As SAPFunctionsOCX.Parameter
  59. Dim iDelimiter As SAPFunctionsOCX.Parameter
  60. Dim iNo_Data As SAPFunctionsOCX.Parameter
  61. Dim iRowSkips As SAPFunctionsOCX.Parameter
  62. Dim iRowCounts As SAPFunctionsOCX.Parameter
  63.  
  64. 'Export
  65.  
  66. 'Tables
  67. Dim tOptions  As SAPTableFactoryCtrl.Table
  68. Dim tFields  As SAPTableFactoryCtrl.Table
  69. Dim tData  As SAPTableFactoryCtrl.Table
  70.    
  71.    
  72.  'Import Parameter of rfc function module
  73.     Set iQuery_Table = Func.Exports("QUERY_TABLE")
  74.     Set iDelimiter = Func.Exports("DELIMITER")
  75.     Set iNo_Data = Func.Exports("NO_DATA")
  76.     Set iRowSkips = Func.Exports("ROWSKIPS")
  77.     Set iRowCounts = Func.Exports("ROWCOUNT")
  78.    
  79.     iDelimiter.Value = vbTab
  80.        
  81.  'Export Parameter of rfc function module
  82.     'not used by rfc_read_table
  83.  
  84.  'Tables rfc function module
  85.     Set tFields = Func.Tables("FIELDS")
  86.     Set tOptions = Func.Tables("OPTIONS")
  87.     Set tData = Func.Tables("DATA")
  88.    
  89.  'Assign values to Import parameter
  90.     iQuery_Table.Value = "T001W"
  91.    
  92.  'Assign values to Import tables
  93.     tOptions.AppendRow
  94.     tOptions(1, "TEXT") = "WERKS = '8000'"   'max 72 char
  95.    
  96.     tFields.AppendRow
  97.     tFields(1, "FIELDNAME") = "WERKS"
  98.    
  99.     tFields.AppendRow
  100.     tFields(2, "FIELDNAME") = "NAME1"
  101.    
  102.     Func.Call
  103.     Debug.Print tData.RowCount
  104.     imax = tData.RowCount
  105.     For ix = 1 To imax
  106.        Debug.Print tData(ix, 1)
  107.        i = 0
  108.         T = Split(tData(ix, 1), vbTab)
  109.         Do While i < tFields.RowCount
  110.         Debug.Print T(i)
  111.         i = i + 1
  112.        Loop
  113.     Next
  114.    
  115.  'Release rfc function
  116.  Functions.RemoveAll
  117.    
  118. End Sub
  119.  
  120.  
  121. Private Sub Main2()
  122.     Dim ix As Long
  123.     Dim imax As Long
  124.    
  125.     Dim T() As String
  126.     Dim i As Long
  127.     Dim k As Long
  128.    
  129.  'Assign SAP rfc function module
  130.     Set Func = Functions.Add("RFC_READ_TABLE")
  131.    
  132.     'Func.Exports("DELIMITER") = vbTab
  133.  
  134. 'Import Paramteres
  135. Dim iQuery_Table As SAPFunctionsOCX.Parameter
  136. Dim iDelimiter As SAPFunctionsOCX.Parameter
  137. Dim iNo_Data As SAPFunctionsOCX.Parameter
  138. Dim iRowSkips As SAPFunctionsOCX.Parameter
  139. Dim iRowCounts As SAPFunctionsOCX.Parameter
  140.  
  141. 'Export
  142.  
  143. 'Tables
  144. Dim tOptions  As SAPTableFactoryCtrl.Table
  145. Dim tFields  As SAPTableFactoryCtrl.Table
  146. Dim tData  As SAPTableFactoryCtrl.Table
  147.    
  148.    
  149.  'Import Parameter of rfc function module
  150.     Set iQuery_Table = Func.Exports("QUERY_TABLE")
  151.     Set iDelimiter = Func.Exports("DELIMITER")
  152.     Set iNo_Data = Func.Exports("NO_DATA")
  153.     Set iRowSkips = Func.Exports("ROWSKIPS")
  154.     Set iRowCounts = Func.Exports("ROWCOUNT")
  155.        
  156.     iDelimiter.Value = vbTab
  157.        
  158.        
  159.  'Export Parameter of rfc function module
  160.     'not used by rfc_read_table
  161.  
  162.  'Tables rfc function module
  163.     Set tFields = Func.Tables("FIELDS")
  164.     Set tOptions = Func.Tables("OPTIONS")
  165.     Set tData = Func.Tables("DATA")
  166.    
  167.  'Assign values to Import parameter
  168.     iQuery_Table.Value = "MSEG"
  169.    
  170.  'Assign values to Import tables
  171.     tOptions.AppendRow
  172.     tOptions(1, "TEXT") = "MBLNR EQ '4900589198' AND MJAHR EQ '2018' AND BWART EQ '412'"   'max 72 char
  173.    
  174.         tFields.AppendRow
  175.         tFields(1, "FIELDNAME") = "MBLNR"
  176.         tFields.AppendRow
  177.         tFields(2, "FIELDNAME") = "MJAHR"
  178.         tFields.AppendRow
  179.         tFields(3, "FIELDNAME") = "ZEILE"
  180.         tFields.AppendRow
  181.         tFields(4, "FIELDNAME") = "BWART"
  182.         tFields.AppendRow
  183.         tFields(5, "FIELDNAME") = "MATNR"
  184.         tFields.AppendRow
  185.         tFields(6, "FIELDNAME") = "WERKS"
  186.         tFields.AppendRow
  187.         tFields(7, "FIELDNAME") = "LGORT"
  188.         tFields.AppendRow
  189.         tFields(8, "FIELDNAME") = "CHARG"
  190.         tFields.AppendRow
  191.         tFields(9, "FIELDNAME") = "SOBKZ"
  192.         tFields.AppendRow
  193.         tFields(10, "FIELDNAME") = "LIFNR"
  194.         tFields.AppendRow
  195.         tFields(11, "FIELDNAME") = "KUNNR"
  196.         tFields.AppendRow
  197.         tFields(12, "FIELDNAME") = "KDAUF"
  198.         tFields.AppendRow
  199.         tFields(13, "FIELDNAME") = "KDPOS"
  200.         tFields.AppendRow
  201.         tFields(14, "FIELDNAME") = "PLPLA"
  202.         tFields.AppendRow
  203.         tFields(15, "FIELDNAME") = "ERFMG"
  204.         tFields.AppendRow
  205.         tFields(16, "FIELDNAME") = "ERFME"
  206.         tFields.AppendRow
  207.         tFields(17, "FIELDNAME") = "EBELN"
  208.         tFields.AppendRow
  209.         tFields(18, "FIELDNAME") = "EBELP"
  210.         tFields.AppendRow
  211.         tFields(19, "FIELDNAME") = "SGTXT"
  212.         tFields.AppendRow
  213.         tFields(20, "FIELDNAME") = "WEMPF"
  214.         tFields.AppendRow
  215.         tFields(21, "FIELDNAME") = "ABLAD"
  216.         tFields.AppendRow
  217.         tFields(22, "FIELDNAME") = "KOSTL"
  218.         tFields.AppendRow
  219.         tFields(23, "FIELDNAME") = "AUFNR"
  220.         tFields.AppendRow
  221.         tFields(24, "FIELDNAME") = "ANLN1"
  222.         tFields.AppendRow
  223.         tFields(25, "FIELDNAME") = "ANLN2"
  224.         tFields.AppendRow
  225.         tFields(26, "FIELDNAME") = "GJAHR"
  226.         tFields.AppendRow
  227.         tFields(27, "FIELDNAME") = "PS_PSP_PNR"
  228.         tFields.AppendRow
  229.         tFields(28, "FIELDNAME") = "BSTMG"
  230.         tFields.AppendRow
  231.         tFields(29, "FIELDNAME") = "BSTME"
  232.         tFields.AppendRow
  233.         tFields(30, "FIELDNAME") = "MANDT"
  234.    
  235.     Func.Call
  236.     Debug.Print tData.RowCount
  237.     imax = tData.RowCount
  238.     For ix = 1 To imax
  239.        Debug.Print tData(ix, 1)
  240.        i = 0
  241.         T = Split(tData(ix, 1), vbTab)
  242.         Do While i < tFields.RowCount
  243.         Debug.Print T(i)
  244.         i = i + 1
  245.        Loop
  246.     Next
  247.    
  248.  'Release rfc function
  249.   Functions.RemoveAll
  250.  
  251. End Sub
  252.  
  253. Sub Start()
  254. Logon
  255. Main
  256. Main2
  257. Main
  258. Main2
  259. Main
  260. Main2
  261.  Connection.Logoff
  262. End Sub
  263.  
GeSHi ©

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.
Tron
.....
.....
 
Posts: 1112
Joined: Sat Aug 04, 2007 10:21 pm

Return to Tips + Tricks & FAQs

Who is online

Users browsing this forum: No registered users and 1 guest