当前位置:网站首页>VBA calls SAP RFC to read & write data

VBA calls SAP RFC to read & write data

2022-04-23 07:52:00 Willie Y

1、RFC structure

RFC Attribute configuration :

 

RFC Input parameters :

ZSCE_MAT× Table structure : 

 

RFC Output parameters :

2、VBA Code

Public Const StartRow% = 2   ' Data start line 
Public Const EndRow% = 3  ' End of data line 
Public Const DataSheetName$ = "Data"
Public SAP As Object
'******************************************************************************
'* Function Desc:VBA call SAP RFC
'* Author: 
'* Param:
'* Returns:
'******************************************************************************
Sub InputData()
Dim WrtDataResult As String
Dim IMATNR$, ZYIELD$

    If ContectSAP = False Then
        MsgBox " Connect SAP Failure "
        Exit Sub
    End If
    If MsgBox(" Connect SAP success ! Add table " & DataSheetName & " Data import in SAP?", vbYesNo, " Warning ") = vbNo Then
        Exit Sub
    End If
    Call OperationPrompts(" Write data to SAP, Please wait a moment ……")
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    
    With ThisWorkbook.Sheets(DataSheetName)
        For i = StartRow To EndRow
           IMATNR = .Cells(i, 1).Value
           ZYIELD = .Cells(i, 2).Value
           WrtDataResult = CallRFC(IMATNR, ZYIELD)
           .Cells(i, 3).Value = WrtDataResult
        Next i
        ' Sign out 
        SAP.Connection.LOGOFF
        Call OperationPrompts(" Disconnected SAP……")
        UF_Prompt.Label1.Caption = "【 End of program running 】"
        ThisWorkbook.Save
    End With
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True
End Sub

call RFC Core code :

'******************************************************************************
'* Function Desc:VBA call SAP RFC
'* Author: 
'* Param:
'*          MATNR_No:
'*          ZYIELD_value: 
'* Returns:
'******************************************************************************
Public Function CallRFC(MATNR_No$, ZYIELD_Value$) As String
Dim LogFlag As Boolean
Dim Result As String
Dim InputTable As Object
Dim Result1 As Object
Dim Result2 As Object


    Set RFC = SAP.Add("RFC_Z_SCE_CHANGEMATERIAL")
    Set InputTable = RFC.Tables("ZSCE_MAT_GDPW")
    
    'rfc Parameter table assignment 
    With InputTable
        .Rows.Add
        .Value(.RowCount, "MATNR") = MATNR_No
        '.Value(.ROWCOUNT, "VOLUM") = VOLUM_Value
        .Value(.RowCount, "ZYIELD") = ZYIELD_Value
    End With
    
    'call RFC
    LogFlag = RFC.Call
    If LogFlag = True Then
       ' Set the return parameters 
        Set Result1 = RFC.Imports("ZMSGNO")
        Set Result2 = RFC.Imports("ZMESSG")
        CallRFC = MATNR_No & "," & Result1.Value & "," & Result2.Value
    Else
       CallRFC = "RFC Call failed !"
    End If
    
    InputTable.FreeTable
    Set InputTable = Nothing
    Set Result1 = Nothing
    Set Result2 = Nothing
    Set RFC = Nothing
    
End Function

In the code RFC Table structure assignment requires row by row assignment .Exports Parameters are passed to participate in Imports The parameters are similar to .

RFC In call to ,Exports Indicates input RFC Parameters of ,Imports Express RFC Returned call result .

summary :

        On a daily basis ,VBA Read SAP surface , Or call RFC It's more convenient , Don't need to SAP Use ABAP Develop and transmit matching production system , Basically, just change the above code .

For more information, please refer to :ExcelVBA And SAPRFC Interface call instance - Douding net        

版权声明
本文为[Willie Y]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230628041296.html