Skip to Content
0
Former Member
Jun 10, 2016 at 03:21 PM

Call BAPI in VBA

697 Views

Hi everyone,

I've been trying for many hours to get a BAPI call working in VBA. Can someone take a look at the code below and tell me what might be wrong? Thanks!

- The code doesn't crash or return an error. It just says that there are 0 rows in the table.

- I've managed to retrieve data from SAP with another BAPI. So in terms of references and connecting to SAP it should be OK

- When I go to SE37, press execute, manually enter the company code, customer and keydate, and execute agian; I do get 115 items as a result.

Sub BAPIgetAccsList()

Application.ScreenUpdating = False

Dim oSAP As Object

Dim objGetList As Object

Dim objGetListTbl As Object

Dim dRowsTable As Double

Set oSAP = CreateObject("SAP.Functions")

Set wsG = ThisWorkbook.Worksheets("GUI")

'------------------Connect---------------

oSAP.Connection.hostName = CNT_STR_APPLN_SRVR

oSAP.Connection.System = CNT_STR_SYSTEM

oSAP.Connection.SystemNumber = CNT_STR_SYS_NUM

oSAP.Connection.client = CNT_STR_CLIENT

oSAP.Connection.Language = CNT_STR_LOGON_LANG

oSAP.Connection.user = CNT_STR_USR

oSAP.Connection.Password = CNT_STR_PWD

If oSAP.Connection.Logon(1, True) = False Then

MsgBox "Could not connect to SAP using provided details", vbCritical, "BAPI connection error"

End

End If

'-------------------------------------------------------------------------------

Set objGetList = oSAP.Add("BAPI_AR_ACC_GETOPENITEMS")

objGetList.exports("COMPANYCODE") = "1000"

objGetList.exports("CUSTOMER") = "1000"

objGetList.exports("KEYDATE") = "10.06.2016"

objBAPIcall = objGetList.call

If objBAPIcall = True Then

Set objGetListTbl = objGetList.tables("LINEITEMS")

dRowsTable = objGetListTbl.RowCount

If tblR <> 0 Then

For r = 1 To tblR

mdR = mdR + 1

rsltRng.Offset(mdR, 1) = objGetListTbl(r, 1)

rsltRng.Offset(mdR, 2) = objGetListTbl(r, 3)

rsltRng.Offset(mdR, 3) = objGetListTbl(r, 4)

Next r

End If

End If

Set objGetList = Nothing

Set objGetListTbl = Nothing

Set objGetDtls = Nothing

Set objGetDtlsImp = Nothing

oSAP.Connection.Logoff

End Sub