当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
面经总结2
设置了body的最大宽度,但是为什么body的背景颜色还铺满整个页面?
Unity获取真实地理地图应用Terrain笔记
Houdini地形与流体解算(模拟泥石流)
Read and modify the JSON file under the resource folder
大学学习路线规划建议贴
异步的学习
利用网页表格导出EXCEL表格加线框及表格内部间距的问题
Mongodb 启动警告信息处理
对复杂字典Dictionary<T1,T2>排序问题
url转成对象
读取修改resource文件夹下的json文件
Solve the problem of deploying mysql8 in docker with correct password but unable to log in to MySQL
Understanding the role of individual units in a deep neural networks
索引被锁数据无法写入ES问题处理
Dropping Pixels for Adversarial Robustness
Common markdown grammar learning
Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
取得所有点列表中的最大值GetMaxPoint
C# 文本文件的查找及替换(WinForm)








