当前位置:网站首页>VBA調用SAP RFC實現數據讀取&寫入
VBA調用SAP RFC實現數據讀取&寫入
2022-04-23 07:52:00 【Willie Y】
1、RFC結構
RFC屬性配置:
RFC輸入參數:
ZSCE_MAT×錶結構:
RFC輸出參數:
2、VBA代碼
Public Const StartRow% = 2 '數據起始行
Public Const EndRow% = 3 '數據結束行
Public Const DataSheetName$ = "Data"
Public SAP As Object
'******************************************************************************
'* Function Desc:VBA調用SAP RFC
'* Author:
'* Param:
'* Returns:
'******************************************************************************
Sub InputData()
Dim WrtDataResult As String
Dim IMATNR$, ZYIELD$
If ContectSAP = False Then
MsgBox "連接SAP失敗"
Exit Sub
End If
If MsgBox("連接SAP成功!是否將錶" & DataSheetName & "中數據導入SAP?", vbYesNo, "警告") = vbNo Then
Exit Sub
End If
Call OperationPrompts("寫入數據到SAP,請稍候……")
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
'注銷登錄
SAP.Connection.LOGOFF
Call OperationPrompts("已斷開SAP……")
UF_Prompt.Label1.Caption = "【程序運行結束】"
ThisWorkbook.Save
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
調用RFC核心代碼:
'******************************************************************************
'* Function Desc:VBA調用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參數錶賦值
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 Result1 = RFC.Imports("ZMSGNO")
Set Result2 = RFC.Imports("ZMESSG")
CallRFC = MATNR_No & "," & Result1.Value & "," & Result2.Value
Else
CallRFC = "RFC調用失敗!"
End If
InputTable.FreeTable
Set InputTable = Nothing
Set Result1 = Nothing
Set Result2 = Nothing
Set RFC = Nothing
End Function
代碼中RFC錶結構賦值需要一行一行賦值。Exports參數傳參和Imports參數類似。
RFC的調用中,Exports錶示輸入RFC的參數,Imports錶示RFC返回的調用結果。
總結:
日常工作中,VBA讀取SAP錶,或者調用RFC還是比較方便的,不需要在SAP使用ABAP開發並傳輸搭配生產系統,基本上改改上述代碼就可用。
更多信息請參考:ExcelVBA與SAPRFC接口調用實例 - 豆丁網
版权声明
本文为[Willie Y]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230628041296.html
边栏推荐
- State synchronization and frame synchronization
- 【NLP笔记】CRF原理初探
- Encapsulate the debug function of unity
- Double sided shader
- 05 use of array
- 踩坑日记:Unable to process Jar entry [module-info.class]
- The page displays the current time in real time
- Simple random roll call lottery (written under JS)
- 03use of scanner class (console input)
- new的实现
猜你喜欢
对复杂字典Dictionary<T1,T2>排序问题
【NLP笔记】CRF原理初探
js之排他思想及案例
Protobuf 使用
Apache Hudi 如何加速传统的批处理模式?
King glory - unity learning journey
利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
如何展示您的数字作品集:来自创意招聘人员的建议
Dropping Pixels for Adversarial Robustness
Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments
随机推荐
反转链表练习
'NPM' is not an internal or external command, nor is it a runnable program or batch file
Understanding of STL container
SampleCameraFilter
js之作用域、作用域链、全局变量和局部变量
自己封装unity的Debug函数
05 use of array
Understanding the role of individual units in a deep neural networks
Judge whether the beginning and end of the string contain target parameters: startswith() and endswith() methods
利用网页表格导出EXCEL表格加线框及表格内部间距的问题
快排的练习
Encapsulate the debug function of unity
Date object (JS built-in object)
C# 读取注册表
ES6 uses recursion to implement deep copy
State synchronization and frame synchronization
利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)
Nodejs (I) event driven programming
将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
事件系统(二)多播事件