SAP的BAPI和Function Module经常让大家迷惑和混淆。究竟BAPI和Functional Module有什么区别呢?我的理解,BAPI是基于Function Module的,但提供面向对象的接口调用方式。比如说,我们想获得总账科目的期间余额,我们可以通过RFC远程调用BAPI_GL_GETGLACCPERIODBALANCES函数来实现,也可以使用类似GeneralLedger.GetGLAccountPeriodBalances这种面向对象的方式来实现。后者就是BAPI调用,底层上BAPI也是调用的BAPI_GL_GETGLACCPERIODBALANCES函数。
在SAP系统中查看BAPI
在SAP中,我们可以使用事务码:SWO2查看BAPI,并且进行测试。比如我们想获得会计科目的期间余额,我们先找到相关对象:
双击General Ledger前面的小圆圈:
在这个界面中,我们可以查看方法的参数,并进行测试。我们先点击“Parameters”按钮查查参数:
BAPI方法的参数,和FM的参数名并不一定相同。比如AccountBalances参数,在FM中对应的参数名称是ACCOUNT_BALA NCES,以BAPI方式调用,需要用BAPI的参数名称,切记。
BAPI测试
点击工具栏上的“Test/Execute”按钮:
填充input parameter :
执行,得到如下结果:
通过BAPI控件调用SAP BAPI
OK,下面编写接口实现的代码。在外部使用BAPI,关键是BAPI ActiveX控件的使用,对应的文件是wdobapi.ocx,Win7下默认路径:C:\Program Files (x86)\SAP\FrontEnd\SAPgui\wdobapi.ocx。添加对ocx控件的引用。
Option Explicit
Public Sub TestGetACBalacne()
Call Logon
Call DoGetAccBalance("Z900", "0010010100", "2015")
Call logoff
End Sub
Private Sub DoGetAccBalance(cocd As String, glAccount As String, year As String)
' Function: Get account period balances, just like FAGLB03'
Dim bapiControl As New SAPBAPIControlLib.SAPBAPIControl
Dim glObj As Object 'general ledger object'
Dim ret As SAPFunctionsOCX.Structure
Dim acBalance As SAPTableFactoryCtrl.Table
Set bapiControl.Connection = sapConnection ' global variable'
' creating business object'
' return value of GetSAPObject() is of type Object'
' first parameter indicates ObjectType, other ten parameters is objectKey'
Set glObj = bapiControl.GetSAPObject("GeneralLedger")
' Invoke method'
glObj.GetGLAccountPeriodBalances _
CompanyCode:=cocd, _
Glacct:=glAccount, _
fiscalYear:=year, _
CurrencyType:="10", _
AccountBalances:=acBalance, _
Return:=ret
' Error occured'
If ret("TYPE") = "E" Then
Call DebugWriteBapiError(ret)
Exit Sub
End If
If acBalance.rowcount > 0 Then
' Write acBalance internal table in worksheet'
Dim sht As Worksheet
Set sht = ThisWorkbook.Worksheets.Add
sht.Name = sht.Name + "_GLBalances"
Call WriteTable(acBalance, sht)
End If
Set acBalance = Nothing
Set glObj = Nothing
Set bapiControl = Nothing
End Sub
Private Sub DebugWriteBapiError(error As SAPFunctionsOCX.Structure)
Debug.Print "Type:", error.Value("TYPE")
Debug.Print "Code:", error.Value("CODE")
Debug.Print "Message:", error.Value("MESSAGE")
End Sub
程序结果: