SAP接口编程之 RFC系列(10) : DimAs方法

上一个示例中,input parameter都是单值的。如果input parameter是结构型或table型的,就需要使用bapiControl.DimAs()方法定义,否则出错。以Customer.GetList()方法为例(对应的FM: BAPI_CUSTOMER_GETLIST)

RFC-10-1.jpg
RFC-10-2.jpg

以下是代码,注意IdRange参数是一个range table,所以用DimAs方法来定义。

Option Explicit

Public Sub TestGetCustomerList()
    Call Logon
    Call DoGetCustomerList("0", "ZZZZ", "100")
    Call logoff
End Sub

Public Sub DoGetCustomerList(customerFrom As String, customerTo As String, maxRow As String)
    Dim bapiControl As SAPBAPIControlLib.SAPBAPIControl
    Dim customerObj As Object
    Dim customerRng As SAPTableFactoryCtrl.Table  ' IdRange parameter'
    Dim address As SAPTableFactoryCtrl.Table
    Dim ret As SAPFunctionsOCX.Structure

    If sapConnection.IsConnected <> tloRfcConnected Then
        Debug.Print "Please connect to SAP first."
        Exit Sub
    End If

    Set bapiControl = New SAPBAPIControl
    Set bapiControl.Connection = sapConnection

    Set customerObj = bapiControl.GetSAPObject("Customer")

    ' fill IdRange parameter'
    Set customerRng = bapiControl.DimAs(customerObj, "GetList", "IdRange")
    customerRng.AppendRow
    customerRng.Value(1, "SIGN") = "I"
    customerRng.Value(1, "OPTION") = "BT"
    customerRng.Value(1, "LOW") = customerFrom
    customerRng.Value(1, "HIGH") = customerTo

    If maxRow = "" Then
        customerObj.GetList IdRange:=customerRng, _
            AddressData:=address, _
            Return:=ret
    Else
        customerObj.GetList IdRange:=customerRng, _
            AddressData:=address, _
            MaxRows:=maxRow, _
            Return:=ret
    End If

    ' Error occured'
    If ret("TYPE") = "E" Then
        Call DebugWriteBapiError(ret)
        Exit Sub
    End If

    If address.rowcount > 0 Then
        Dim sht As Worksheet
        Set sht = ThisWorkbook.Worksheets.Add
        Call WriteTable(address, sht)
    End If

    Set address = Nothing
    Set customerObj = Nothing
    Set bapiControl = Nothing
End Sub

Private Sub DebugWriteBapiError(error As SAPFunctionsOCX.Structure)
    Debug.Print "Type:", error.Value("TYPE")
    Debug.Print "Class:", error.Value("ID")
    Debug.Print "Number:", error.Value("NUMBER")
    Debug.Print "Message:", error.Value("MESSAGE")
End Sub

DimAs语法:

Function DimAs(Object As Object, Method As String, Parameter As String) As Object
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容