Sapass

 Simplify SAP R/3 development with Excel VBA/VB RFC  
Home> Sap_Active_X>Call SAP RFC Function 1



Index

Call SAP RFC Function 1

Here we will check how we can call SAP RFC enabled function module from VBA/VB. This utilize the ActiveX module in ...SAP/FrontEnd/Controls/wdtfuncs.ocx.


(A) Declaration of Function
After you logged on through SAP.Functions logon method, you can add Function as written below. Here we declare one of generic table access function module RFC_READ_TABLE.
'Logon
Dim sapConn As Object 'Declare variant
Set sapConn = CreateObject("SAP.Functions") 'Create ActiveX object

If sapConn.Connection.Logon(0, False) <> True Then 'Try Logon
  msgbox "Cannot Log on to SAP"
end if
'Define function
Dim objRfcFunc As Object
Set objRfcFunc = sapConn.Add("RFC_READ_TABLE")


(B) Declaration of Export parameter
For declaration of export parameters, you can use "Exports(parmname)". You can simply move string to the parameter if the parameter is not a structure. Same as structure creation, by declaring export parameter, it automatically creates structure. *parmname must be as defined in SE37. In this example, we get Idoc control record up to 10.
Dim objQueryTab, objRowCount As Object
Set objQueryTab = objRfcFunc.Exports("QUERY_TABLE")
objQueryTab.Value = "EDIDC"
Set objRowCount = objRfcFunc.Exports("ROWCOUNT")
objRowCount.Value = "10"


(C) Declaration of Tables parameter
For table parameters, you can do it as below - "Tables(parmname)". *parmname must be as defined in SE37.
Dim objOptTab, objFldTab, objDatTab As Object
Set objOptTab = objRfcFunc.Tables("OPTIONS")
Set objFldTab = objRfcFunc.Tables("FIELDS")
Set objDatTab = objRfcFunc.Tables("DATA")


(D) Set entries to Tables parameter
As written in the Tables section, you can append and set the entries as written below. Here we specify the condition for selecting table entries in OPTIONS table, then specify Fields to obtain in FIELDS table. In this example, we try to select failed ORDERS Idocs.
'First we set the condition
'Refresh table
objOptTab.FreeTable
'Then set values
objOptTab.Rows.Add
objOptTab(objOptTab.RowCount, "TEXT") = "MESTYP = 'ORDERS' and "
objOptTab.Rows.Add
objOptTab(objOptTab.RowCount, "TEXT") = "STATUS = '51'"

'Next we set fields to obtain
'Refresh table
objFldTab.FreeTable
'Then set values
objFldTab.Rows.Add
objFldTab(objFldTab.RowCount, "FIELDNAME") = "DOCNUM"
objFldTab.Rows.Add
objFldTab(objFldTab.RowCount, "FIELDNAME") = "MESTYP"
objFldTab.Rows.Add
objFldTab(objFldTab.RowCount, "FIELDNAME") = "IDOCTP"
objFldTab.Rows.Add
objFldTab(objFldTab.RowCount, "FIELDNAME") = "SNDPRN"
objFldTab.Rows.Add
objFldTab(objFldTab.RowCount, "FIELDNAME") = "SNDPRT"


(E) Call
After all the parameters done, we can now call the function by Call method. If it goes successfully, we get True as a result and False if it failed. In case it failed, an exception message will be returned to Exception property of Function object.
If objRfcFunc.Call = False Then
   MsgBox "Error occured - " & objRfcFunc.Exception
End If


(F) Get result from the returned table
Check DATA table returned by the function call as below with referring to the fields. By the code below, the fields - Idoc number, Message type, Idoc type, Sender partner number and Sender partner type will be populated from Cell(1, 1) in ActiveSheet. In this code, we split the table entry into fields as returned in the FIELD parameter.
Dim objDatRec As Object
Dim objFldRec As Object
For Each objDatRec In objDatTab.Rows
   For Each objFldRec In objFldTab.Rows
      Cells(objDatRec.Index, objFldRec.Index) =
               Mid(objDatRec("WA"), _objFldRec("OFFSET") + 1, objFldRec("LENGTH"))
   Next
Next


(D) Sample code
Here we have complete set of example code. You can cut and paste this into your macro module by Visual Basic Editor in Excel.
Sub GetTable()
'Logon
Dim sapConn As Object 'Declare variant
Set sapConn = CreateObject("SAP.Functions") 'Create ActiveX object

If sapConn.Connection.Logon(0, False) <> True Then 'Try Logon
   MsgBox "Cannot Log on to SAP"
End If
'Define function
Dim objRfcFunc As Object
Set objRfcFunc = sapConn.Add("RFC_READ_TABLE")

Dim objQueryTab, objRowCount As Object
Set objQueryTab = objRfcFunc.Exports("QUERY_TABLE")
objQueryTab.Value = "EDIDC"
Set objRowCount = objRfcFunc.Exports("ROWCOUNT")
objRowCount.Value = "10"

Dim objOptTab, objFldTab, objDatTab As Object
Set objOptTab = objRfcFunc.Tables("OPTIONS")
Set objFldTab = objRfcFunc.Tables("FIELDS")
Set objDatTab = objRfcFunc.Tables("DATA")
'First we set the condition
'Refresh table
objOptTab.FreeTable
'Then set values
objOptTab.Rows.Add
objOptTab(objOptTab.RowCount, "TEXT") = "MESTYP = 'ORDERS' and "
objOptTab.Rows.Add
objOptTab(objOptTab.RowCount, "TEXT") = "STATUS = '53'"

'Next we set fields to obtain
'Refresh table
objFldTab.FreeTable
'Then set values
objFldTab.Rows.Add
objFldTab(objFldTab.RowCount, "FIELDNAME") = "DOCNUM"
objFldTab.Rows.Add
objFldTab(objFldTab.RowCount, "FIELDNAME") = "MESTYP"
objFldTab.Rows.Add
objFldTab(objFldTab.RowCount, "FIELDNAME") = "IDOCTP"
objFldTab.Rows.Add
objFldTab(objFldTab.RowCount, "FIELDNAME") = "SNDPRN"
objFldTab.Rows.Add
objFldTab(objFldTab.RowCount, "FIELDNAME") = "SNDPRT"

If objRfcFunc.Call = False Then
   MsgBox objRfcFunc.Exception
End If

Dim objDatRec As Object
Dim objFldRec As Object
For Each objDatRec In objDatTab.Rows
   For Each objFldRec In objFldTab.Rows
      Cells(objDatRec.Index, objFldRec.Index) =
            Mid(objDatRec("WA"), objFldRec("OFFSET") + 1, objFldRec("LENGTH"))
   Next
Next

End Sub


17-Sep-2005