1

I want to call an ABAP function from an Excel VBA Macro. Is there any method I can follow to achieve this.

Please help me regarding this.

1 Answer 1

2
Dim sapConn As Object 'Declare connection object
Set sapConn = CreateObject("SAP.Functions") 'Create ActiveX object

sapConn.Connection.user = "user"         'Specify user
sapConn.Connection.Password = ""                'Then password
sapConn.Connection.client = "001"               'Client
sapConn.Connection.ApplicationServer = "server" 'Target server address
sapConn.Connection.Language = "PT"              'Language code

'Finally, try to logon to the specified system and check if the connection established
If sapConn.Connection.Logon(0, True) <> True Then
  MsgBox "Cannot Log on to SAP" 'Issue message if cannot logon
Else
  MsgBox "Logged on to SAP!"
End If


Dim rfcAcctDocCheck As Object
Dim oAcctHeader As Object
Dim otAcctAR, otAcctGL, otAcctAP, otAcctAMT, otReturn As Object

Set rfcAcctDocCheck = sapConn.Add("BAPI_ACC_DOCUMENT_CHECK")
Set oAcctHeader = rfcAcctDocCheck.Exports("DOCUMENTHEADER")

Set otAcctGL = rfcAcctDocCheck.Tables("ACCOUNTGL")
Set otAcctAR = rfcAcctDocCheck.Tables("ACCOUNTRECEIVABLE")
Set otAcctAP = rfcAcctDocCheck.Tables("ACCOUNTPAYABLE")
Set otAcctAMT = rfcAcctDocCheck.Tables("CURRENCYAMOUNT")
Set otReturn = rfcAcctDocCheck.Tables("RETURN")

Dim qtLegs As Integer

Dim dt, comp, tpDoc, docRef, tpAcct, acct, customer, vendor, _
    curr, val, spLedger, ccenter, order As String
Dim curLine As Integer

For lin = 1 To UBound(reg)
    id = Format(tbPost.Cells(reg(lin).lin_ini, K_COL_ID), "0000000000")
    dt = getDate(tbPost.Cells(reg(lin).lin_ini, K_COL_DT))
    comp = getCompanyCode(tbPost.Cells(reg(lin).lin_ini, K_COL_EMPR))
    tpDoc = getDocumentType(tbPost.Cells(reg(lin).lin_ini, K_COL_TP_DOC))
    docRef = tbPost.Cells(reg(lin).lin_ini, K_COL_DOC_REF)

    otAcctGL.freeTable
    otAcctAR.freeTable
    otAcctAP.freeTable
    otAcctAMT.freeTable

oAcctHeader("USERNAME") = sapConn.Connection.user
oAcctHeader("HEADER_TXT") = "Lancado via Excel"
oAcctHeader("COMP_CODE") = comp
oAcctHeader("DOC_DATE") = dt
oAcctHeader("PSTNG_DATE") = dt
oAcctHeader("DOC_TYPE") = tpDoc
oAcctHeader("REF_DOC_NO") = docRef

otAcctAMT.Rows.Add
otAcctAMT(otAcctAMT.Rows.Count, "ITEMNO_ACC") = Format(leg, "0000000000")
otAcctAMT(otAcctAMT.Rows.Count, "CURRENCY") = curr
otAcctAMT(otAcctAMT.Rows.Count, "AMT_BASE") = val
Next

If rfcAcctDocCheck.Call = False Then
    MsgBox rfcAcctDocCheck.Exception
End If
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your solution. I will work on this. I also wanted to know are there any methods using which I can just call abap functions directly. Means there are some ABAP functions which are performing some operations, I need to just call that abap function from VBA. Could you please let me know about this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.