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接口调用实例 - 豆丁网? ? ? ?
|