转载出处:http://blog.csdn.net/stone0823 https://blog.csdn.net/stone0823/article/details/50669065
BAPI控件的DimAs方法
上一个示例中,input parameter都是单值的。如果input parameter是结构型或table型的,就需要使用bapiControl.DimAs()方法定义,否则出错。以Customer.GetList()方法为例(对应的FM: BAPI_CUSTOMER_GETLIST)
以下是代码,注意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
(∩˃o˂∩)♡