Skip to Content
author's profile photo Former Member
Former Member

Coding Error

Hi Experts

Regarding Create Sales order in SAP through VB, i have done the following code which collected from the forum.

Now it gives the run time error as 'Object Required' in the line

Set boOrder = oBAPICtrl.GetSAPObject("SalesOrder")

What could be the reason actually, pls help me on this issue.

Do i need SalesOrder object to be defined here, if so how and where pls help me.

Coding

Dim boOrder As Object 'Business object SalesOrder

Dim oPartners As Object 'Parameter OrderPartners of BAPI method

Dim oHeader As Object 'Parameter OrderHeaderIn of BAPI method

Dim oItemsIn As Object 'Parameter OrderItemsIn of BAPI method

Dim oReturn As Object 'Parameter Return of BAPI method

Private Sub Form_Load()

'Connect to business object SalesOrder

'(this creates an anonymous object with an empty key field):

Set boOrder = oBAPICtrl.GetSAPObject("SalesOrder")

'Get structure/table objects:

Set oPartners = oBAPICtrl.DimAs(boOrder, "CreateFromData", "OrderPartners")

Set oHeader = oBAPICtrl.DimAs(boOrder, "CreateFromData", "OrderHeaderIn")

Set oItemsIn = oBAPICtrl.DimAs(boOrder, "CreateFromData", "OrderItemsIn")

'Fill header:

oHeader.Value("DOC_TYPE") = "TA" 'Standard order

oHeader.Value("SALES_ORG") = "0001" 'Sales organization

oHeader.Value("DISTR_CHAN") = "01" 'Sales channel

oHeader.Value("DIVISION") = "01" 'Division

oHeader.Value("PO_NUMBER") = "" 'Customer purchase orderNumber

oHeader.Value("PRICE_DATE") = Now 'Date

'Fill partners:

oPartners.Rows.Add

oPartners.Value(1, "PARTN_ROLE") = "AG" 'PartnerRoll: Person posting the order

oPartners.Value(1, "PARTN_NUMB") = "0000010096" 'Customer number

'Fill items:

oItemsIn.Rows.Add

oItemsIn.Value(1, "REQ_QTY") = "0000000010000" 'Quantity

oItemsIn.Value(1, "MATERIAL") = "BERLINER" 'Product ID

oItemsIn.Value(1, "COND_VALUE") = "1432" 'Rate

'Call the method:

boOrder.CreateFromData OrderHeaderIn:=oHeader, _

OrderPartners:=oPartners, _

OrderItemsIn:=oItemsIn, _

Return:=oReturn

'Free the business objects:

Set boOrder = Nothing

End Sub

Thanks in advance.

Regards

Rajaram

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • author's profile photo Former Member
    Former Member
    Posted on Jan 19, 2008 at 07:19 AM

    Hi

    First make a .DLL with DCOM using a table read BAPI.

    You will need to do a BAPI search in SAP to find the exact name in your SAP instance.

    To use the BAPI there are Three parts.

    Part 1. Get a SAP session in the VB program.

    Part 2. Define the table, fields, and selection criteria

    Part 3. Convert the delimited data into a recordset.

    Here is a code sample we have put together to use BAPI from MS Access, but VB concept should hold. This sample logs into SAP, adds records and retrieves SAP document numbers from uploaded records. Getting any examples of this from anywhere is a real nightmare.

    Hope this helps people.

    Option Compare Database

    Option Explicit

    'Constants

    Const mstrSAPLogicalSystem As String = "FinCBE"

    Const mstrSAPInvoiceObjType As String = "IBKPF"

    Const mstrSAPInvoiceDocType As String = "KR"

    Const mstrSAPCreditDocType As String = "KG"

    Const mstrSAPAccountKey As String = "VST"

    Const mstrSAPInvoiceCurrency As String = "GBP"

    Const mstrSAPReversalReason As String = "Z1"

    'BAPI Controls

    Public oBAPICtrl As Object

    Public oSAPConnection As Object

    'BAPI functions

    Dim oPostInvoice As Object

    Dim oBAPIService As Object

    Dim oVendorStatement As Object

    'Input structures

    Dim oDocumentHeader As Object

    Dim oAccountPayable As Object

    Dim oAccountGL As Object

    Dim oAccountTax As Object

    Dim oCurrencyAmount As Object

    Dim oReverseInvoice As Object

    'Output Structures

    Dim oInvoiceReturn As Object

    Dim oCommitReturn As Object

    Dim oStmtReturn As Object

    Dim oLineItems As Object

    ' Dummy structure to hold initialised BAPI structures, prevents another call to the BAPI

    Type iniBAPI

    DocumentHeader As Object

    AccountPayable As Object

    AccountGL As Object

    AccountTax As Object

    CurrencyAmount As Object

    InvoiceReturn As Object

    CommitReturn As Object

    StmtReturn As Object

    LineItems As Object

    ReverseInvoice As Object

    End Type

    'Global variables

    Public gstrBAPIMessage As String

    'Local module variables

    Dim mSuccess As Boolean

    Dim mLastSAPDoc As String

    Dim mdfBAPI As iniBAPI

    Dim mvarReturn

    Public Function SAPLogin(Optional abUnattended As Boolean = False) As Boolean

    ' -


    ' MODULE: SAPLogin

    ' AUTHOR: Nick Fry

    ' NARRATIVE: Establishes a connection with SAP

    ' HISTORY: 11-Feb-2005 NF Created

    '

    ' OWNERS: B&Q PLC

    ' -


    '

    ' Parameters reqd : abAttended - Is this an Attended or Unattended login

    Dim lConnect As Long

    Set oBAPICtrl = Nothing

    Set oSAPConnection = Nothing

    Set oPostInvoice = Nothing

    Set oBAPIService = Nothing

    Set oVendorStatement = Nothing

    Set oBAPICtrl = CreateObject("SAP.BAPI.1")

    Set oSAPConnection = oBAPICtrl.Connection

    If gstrSAPDebug Then

    oSAPConnection.TraceLevel = gstrSAPTraceLevel

    oBAPICtrl.LogLevel = gstrSAPLogLevel

    oBAPICtrl.LogFileName = gstrSAPLogFile

    End If

    oSAPConnection.ApplicationServer = gstrSAPLogonAppServer

    oSAPConnection.Client = gstrSAPLogonClient

    oSAPConnection.Destination = gstrSAPLogonDest

    oSAPConnection.System = gstrSAPLogonSystem

    oSAPConnection.SystemID = gstrSAPLogonSystemID

    If abUnattended Then

    oSAPConnection.User = gstrSAPStagUser

    oSAPConnection.Password = gstrSAPStagPswd

    ' Else

    ' DoCmd.OpenForm ("frmSAPLogin")

    End If

    'Perform a remote logon to the R/3 System

    lConnect = 0

    SAPLogin = oSAPConnection.Logon(lConnect, abUnattended)

    End Function

    Public Function ReverseInvoices(aoRS As ADODB.Recordset, auDoc() As ReturnDoc, abBatch As Boolean, Optional aLog As Integer, Optional asLogFile As String = "")

    ' -


    ' MODULE: ReverseInvoices

    ' AUTHOR: Nick Fry

    ' NARRATIVE: Excute SAP BAPI function to reverse existing invoices

    ' from a recordset and return the document numbers

    ' HISTORY: 11-Feb-2005 NF Created

    '

    ' OWNERS: B&Q PLC

    ' -


    '

    ' Parameters reqd : abAttended - Is this an Attended or Unattended login

    Dim vObjType, vObjKey, vObjSys

    Dim bCheckReverse As Boolean

    Dim lCount As Long

    Dim lSuccess As Long

    Dim lFailure As Long

    Dim i As Long

    Dim strErr As String

    If oPostInvoice Is Nothing Then

    Set oPostInvoice = oBAPICtrl.GetSAPObject("AcctngInvoiceReceipt")

    End If

    'Set up general services

    If oBAPIService Is Nothing Then

    Set oBAPIService = oBAPICtrl.GetSAPObject("BapiService")

    End If

    Do Until aoRS.EOF

    ReDim Preserve auDoc(lCount)

    ' Check SAP connection and attempt to re-establish if broken

    If oSAPConnection Is Nothing Then

    mSuccess = SAPLogin(abBatch)

    If mSuccess Then

    If oPostInvoice Is Nothing Then

    Set oPostInvoice = oBAPICtrl.GetSAPObject("AcctngInvoiceReceipt")

    End If

    If oBAPIService Is Nothing Then

    Set oBAPIService = oBAPICtrl.GetSAPObject("BapiService")

    End If

    Else

    Err.Raise 50010, "ReverseInvoices", "SAPLogin - unable to re-establish connection"

    End If

    End If

    'Reinitialise structures

    Set oReverseInvoice = Nothing

    Set oInvoiceReturn = Nothing

    Set oCommitReturn = Nothing

    Set oReverseInvoice = oBAPICtrl.DimAs(oPostInvoice, "Reverse", "Reversal")

    Set oInvoiceReturn = oBAPICtrl.DimAs(oPostInvoice, "Reverse", "Return")

    Set oCommitReturn = oBAPICtrl.DimAs(oBAPIService, "TransactionCommit", "Return")

    'Set up the reversal structure

    oReverseInvoice.Value("OBJ_TYPE") = mstrSAPInvoiceObjType

    oReverseInvoice.Value("OBJ_KEY") = aoRS!DocumentRef

    oReverseInvoice.Value("OBJ_SYS") = mstrSAPLogicalSystem

    oReverseInvoice.Value("OBJ_KEY_R") = aoRS!DocumentRef

    oReverseInvoice.Value("PSTNG_DATE") = CDate(aoRS!PostingDate)

    oReverseInvoice.Value("FIS_PERIOD") = aoRS!FisPeriod

    oReverseInvoice.Value("COMP_CODE") = aoRS!CompanyCode

    oReverseInvoice.Value("REASON_REV") = mstrSAPReversalReason

    oReverseInvoice.Value("AC_DOC_NO") = aoRS!SAPDocumentNo

    'Reverse the invoice

    oPostInvoice.Reverse Reversal:=oReverseInvoice, _

    ObjType:=vObjType, _

    ObjKey:=vObjKey, _

    Return:=oInvoiceReturn, _

    ObjSys:=vObjSys

    'Handle errors at the remote call

    bCheckReverse = True

    If Not (oInvoiceReturn Is Nothing) Then

    gstrBAPIMessage = ""

    For i = 1 To oInvoiceReturn.rowCount

    If oInvoiceReturn.Value(i, "TYPE") <> "" And oInvoiceReturn.Value(i, "TYPE") <> "S" Then

    bCheckReverse = False

    End If

    gstrBAPIMessage = gstrBAPIMessage + vbCRLF + "* " + oInvoiceReturn.Value(i, "TYPE") + oInvoiceReturn.Value(i, "NUMBER") + ": " + oInvoiceReturn.Value(i, "MESSAGE") + "(" + oInvoiceReturn.Value(i, "MESSAGE_V1") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V2") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V3") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V4") + ")"

    strErr = "* " + oInvoiceReturn.Value(i, "TYPE") + oInvoiceReturn.Value(i, "NUMBER") + ": " + oInvoiceReturn.Value(i, "MESSAGE") + "(" + oInvoiceReturn.Value(i, "MESSAGE_V1") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V2") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V3") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V4") + ")"

    Next

    End If

    If gstrBAPIMessage <> "" Then

    gstrBAPIMessage = vbCRLF & "Reversing " & aoRS!DocumentText & gstrBAPIMessage

    If Not (asLogFile = "") Then Print #aLog, gstrBAPIMessage

    If Not abBatch And Not bCheckReverse Then MsgBox gstrBAPIMessage, vbInformation, "Check Invoice"

    End If

    If bCheckReverse Then

    ' Commit the reverse so that we can get the document number

    oBAPIService.TransactionCommit Wait:="X", _

    Return:=oCommitReturn

    ' Key the vendor statement object to the current vendor

    Set oVendorStatement = Nothing

    Set oVendorStatement = oBAPICtrl.GetSAPObject("APAccount", aoRS!CompanyCode, aoRS!VendorNumber)

    Set oLineItems = oBAPICtrl.DimAs(oVendorStatement, "GetBalancedItems", "LineItems")

    Set oStmtReturn = oBAPICtrl.DimAs(oVendorStatement, "GetBalancedItems", "Return")

    ' Retrieve a statement of balanced items for the vendor

    oVendorStatement.GetBalancedItems DateFrom:=oReverseInvoice.Value("PSTNG_DATE"), _

    DateTo:=oReverseInvoice.Value("PSTNG_DATE"), _

    LineItems:=oLineItems, _

    Return:=oStmtReturn

    ' Loop through the items to match the one just posted

    For i = oLineItems.rowCount To 1 Step -1

    If UCase(oLineItems.Value(i, "ALLOC_NMBR")) = UCase(oReverseInvoice.Value("OBJ_KEY_R")) _

    And oLineItems.Value(i, "DOC_NO") = aoRS!SAPDocumentNo Then

    mLastSAPDoc = oLineItems.Value(i, "REVERSAL_DOC")

    If Not (asLogFile = "") Then Print #aLog, "Document reversed on " & mLastSAPDoc

    Exit For

    End If

    Next

    ' Store the result for return

    lSuccess = lSuccess + 1

    auDoc(lCount).ID = aoRS!InvoiceID

    auDoc(lCount).Success = True

    auDoc(lCount).SAPDocNo = aoRS!SAPDocumentNo

    auDoc(lCount).SAPRevNo = mLastSAPDoc

    auDoc(lCount).FailReason = ""

    Else

    ' Store the result for return

    lFailure = lFailure + 1

    auDoc(lCount).ID = aoRS!InvoiceID

    auDoc(lCount).Success = False

    auDoc(lCount).SAPDocNo = aoRS!SAPDocumentNo

    auDoc(lCount).FailReason = strErr

    End If

    mvarReturn = SysCmd(acSysCmdUpdateMeter, lCount)

    lCount = lCount + 1

    aoRS.MoveNext

    Loop

    If Not (asLogFile = "") Then Print #aLog, vbCRLF & "Reverse Invoices: Processed " & Format(lCount, "#,##0") & ", Reversed: " & Format(lSuccess, "#,##0") & ", Failed: " & Format(lFailure, "#,##0") & vbCRLF

    ReverseInvoices = True

    End Function

    Public Function PostNewInvoices(aoRS As ADODB.Recordset, auDoc() As ReturnDoc, abBatch As Boolean, Optional aLog As Integer, Optional asLogFile As String = "")

    ' -


    ' MODULE: PostNewInvoices

    ' AUTHOR: Nick Fry

    ' NARRATIVE: Excute SAP BAPI function to check and post new invoices

    ' from a recordset and return the document numbers

    ' HISTORY: 11-Feb-2005 NF Created

    '

    ' OWNERS: B&Q PLC

    ' -


    '

    ' Parameters reqd : aoRS - Recordset of new invoices or credit notes to be posted

    ' auDoc - structure in which to return posted invoice numbers

    ' abBatch - attended or unattended logon

    ' aLog+asLogFile - name and number of logfile if required

    PostNewInvoices = False

    Dim sLastRef As String

    Dim sLastSAPDoc As String

    Dim lRead As Long

    Dim lCount As Long

    Dim lSuccess As Long

    Dim lFailure As Long

    Dim lItem As Long

    Dim lAPRow As Long

    Dim lGLRow As Long

    Dim lTaxRow As Long

    Dim bPost As Boolean

    bPost = False

    lCount = 0

    lItem = 0

    lAPRow = 0

    lGLRow = 0

    lTaxRow = 0

    lRead = 1

    Do Until aoRS.EOF

    ReDim Preserve auDoc(lCount)

    ' Check SAP connection and attempt to re-establish if broken

    If oSAPConnection Is Nothing Then

    mSuccess = SAPLogin(abBatch)

    If mSuccess Then

    Call InitInvoiceStructures

    sLastRef = ""

    bPost = False

    Else

    Err.Raise 50010, "PostNewInvoices", "SAPLogin - unable to re-establish connection"

    End If

    End If

    If aoRS!InvoiceID <> sLastRef Then

    If bPost Then

    mSuccess = PostInvoice(oDocumentHeader.Value("COMP_CODE"), oAccountPayable.Cell(1, "VENDOR_NO"), abBatch, aLog, asLogFile)

    auDoc(lCount).ID = sLastRef

    If mSuccess Then

    auDoc(lCount).Success = True

    lSuccess = lSuccess + 1

    If sLastSAPDoc = "" Then

    auDoc(lCount).SAPDocNo = mLastSAPDoc

    auDoc(lCount).SAPRevNo = ""

    Else

    auDoc(lCount).SAPDocNo = sLastSAPDoc

    auDoc(lCount).SAPRevNo = mLastSAPDoc

    End If

    auDoc(lCount).FailReason = ""

    Else

    lFailure = lFailure + 1

    auDoc(lCount).Success = False

    auDoc(lCount).SAPDocNo = sLastSAPDoc

    auDoc(lCount).FailReason = gstrBAPIMessage

    End If

    bPost = False

    lCount = lCount + 1

    lItem = 0

    lAPRow = 0

    lGLRow = 0

    lTaxRow = 0

    End If

    'Reset structures

    Call InitInvoiceStructures

    'Store this invoice reference

    sLastRef = aoRS!InvoiceID

    sLastSAPDoc = aoRS!SAPDocumentNo

    End If

    If Not bPost Then

    ' Store the header and Vendor details

    ' Header Details

    oDocumentHeader.Value("OBJ_TYPE") = mstrSAPInvoiceObjType

    oDocumentHeader.Value("OBJ_KEY") = aoRS!DocumentRef

    oDocumentHeader.Value("OBJ_SYS") = mstrSAPLogicalSystem

    oDocumentHeader.Value("USERNAME") = gfsNetworkUserID

    oDocumentHeader.Value("HEADER_TXT") = aoRS!HeaderText

    oDocumentHeader.Value("COMP_CODE") = aoRS!CompanyCode

    oDocumentHeader.Value("FISC_YEAR") = aoRS!FisYear

    oDocumentHeader.Value("DOC_DATE") = CDate(aoRS!DocumentDate)

    oDocumentHeader.Value("PSTNG_DATE") = CDate(aoRS!PostingDate)

    oDocumentHeader.Value("FIS_PERIOD") = aoRS!FisPeriod

    If aoRS!SAPDocumentNo = "" Then

    oDocumentHeader.Value("DOC_TYPE") = mstrSAPInvoiceDocType

    Else

    oDocumentHeader.Value("DOC_TYPE") = mstrSAPCreditDocType

    End If

    oDocumentHeader.Value("REF_DOC_NO") = aoRS!DocumentRef

    'Vendor details

    lItem = lItem + 1

    lAPRow = lAPRow + 1

    oAccountPayable.Rows.Add

    oAccountPayable.Cell(lAPRow, "ITEMNO_ACC") = lItem

    oAccountPayable.Cell(lAPRow, "VENDOR_NO") = aoRS!VendorNumber

    oAccountPayable.Cell(lAPRow, "ITEM_TEXT") = aoRS!DocumentText

    'Currency row for vendor line

    oCurrencyAmount.Rows.Add

    oCurrencyAmount.Cell(lItem, "ITEMNO_ACC") = lItem

    oCurrencyAmount.Cell(lItem, "CURRENCY") = mstrSAPInvoiceCurrency

    oCurrencyAmount.Cell(lItem, "AMT_DOCCUR") = aoRS!VendorAmount

    oCurrencyAmount.Cell(lItem, "AMT_BASE") = aoRS!GLAmount

    bPost = True

    End If

    ' GL Details

    lItem = lItem + 1

    lGLRow = lGLRow + 1

    oAccountGL.Rows.Add

    oAccountGL.Cell(lGLRow, "ITEMNO_ACC") = lItem

    oAccountGL.Cell(lGLRow, "GL_ACCOUNT") = aoRS!GLAccount

    oAccountGL.Cell(lGLRow, "TAX_CODE") = aoRS!TaxCode

    oAccountGL.Cell(lGLRow, "FISC_YEAR") = aoRS!FisYear

    oAccountGL.Cell(lGLRow, "FIS_PERIOD") = aoRS!FisPeriod

    oAccountGL.Cell(lGLRow, "ITEM_TEXT") = aoRS!DocumentText

    oAccountGL.Cell(lGLRow, "COSTCENTER") = aoRS!CostCentre

    'Currency row for GL line

    oCurrencyAmount.Rows.Add

    oCurrencyAmount.Cell(lItem, "ITEMNO_ACC") = lItem

    oCurrencyAmount.Cell(lItem, "CURRENCY") = mstrSAPInvoiceCurrency

    oCurrencyAmount.Cell(lItem, "AMT_DOCCUR") = aoRS!GLAmount

    oCurrencyAmount.Cell(lItem, "AMT_BASE") = aoRS!GLAmount

    ' Tax Details

    lItem = lItem + 1

    lTaxRow = lTaxRow + 1

    oAccountTax.Rows.Add

    oAccountTax.Cell(lTaxRow, "ITEMNO_ACC") = lItem

    oAccountTax.Cell(lTaxRow, "TAX_CODE") = aoRS!TaxCode

    oAccountTax.Cell(lTaxRow, "GL_ACCOUNT") = aoRS!TaxAccount

    oAccountTax.Cell(lTaxRow, "ACCT_KEY") = mstrSAPAccountKey

    'Currency row for GL line

    oCurrencyAmount.Rows.Add

    oCurrencyAmount.Cell(lItem, "ITEMNO_ACC") = lItem

    oCurrencyAmount.Cell(lItem, "CURRENCY") = mstrSAPInvoiceCurrency

    oCurrencyAmount.Cell(lItem, "AMT_DOCCUR") = aoRS!TaxAmount

    oCurrencyAmount.Cell(lItem, "AMT_BASE") = aoRS!GLAmount

    mvarReturn = SysCmd(acSysCmdUpdateMeter, lRead)

    aoRS.MoveNext

    If Not aoRS.EOF Then lRead = lRead + 1

    Loop

    ' Is there one last item to output?

    If bPost Then

    mSuccess = PostInvoice(oDocumentHeader.Value("COMP_CODE"), oAccountPayable.Cell(1, "VENDOR_NO"), abBatch, aLog, asLogFile)

    ReDim Preserve auDoc(lCount)

    auDoc(lCount).ID = sLastRef

    If mSuccess Then

    lSuccess = lSuccess + 1

    auDoc(lCount).Success = True

    If sLastSAPDoc = "" Then

    auDoc(lCount).SAPDocNo = mLastSAPDoc

    auDoc(lCount).SAPRevNo = ""

    Else

    auDoc(lCount).SAPDocNo = sLastSAPDoc

    auDoc(lCount).SAPRevNo = mLastSAPDoc

    End If

    auDoc(lCount).FailReason = ""

    Else

    lFailure = lFailure + 1

    auDoc(lCount).Success = False

    auDoc(lCount).SAPDocNo = sLastSAPDoc

    auDoc(lCount).FailReason = gstrBAPIMessage

    End If

    End If

    If Not (asLogFile = "") Then Print #aLog, vbCRLF & "Post Invoices/CreditNotes: Processed " & Format(lRead, "#,##0") & ", Posted: " & Format(lSuccess, "#,##0") & ", Failed: " & Format(lFailure, "#,##0") & vbCRLF

    PostNewInvoices = True

    End Function

    Private Sub InitInvoiceStructures()

    ' -


    ' MODULE: InitInvoiceStructures

    ' AUTHOR: Nick Fry

    ' NARRATIVE: Initializes the BAPI structures used to Post Invoices

    ' from a recordset and return the document numbers

    ' HISTORY: 11-Feb-2005 NF Created

    '

    ' OWNERS: B&Q PLC

    ' -


    '

    ' Parameters reqd : abAttended - Is this an Attended or Unattended login

    'Create local instance of BAPI

    If oPostInvoice Is Nothing Then

    Set oPostInvoice = oBAPICtrl.GetSAPObject("AcctngInvoiceReceipt")

    End If

    'Set up general services

    If oBAPIService Is Nothing Then

    Set oBAPIService = oBAPICtrl.GetSAPObject("BapiService")

    End If

    'Destroy current structures

    Set oDocumentHeader = Nothing

    Set oAccountPayable = Nothing

    Set oAccountGL = Nothing

    Set oAccountTax = Nothing

    Set oCurrencyAmount = Nothing

    Set oInvoiceReturn = Nothing

    Set oCommitReturn = Nothing

    'Create a new table or structure

    Set oDocumentHeader = oBAPICtrl.DimAs(oPostInvoice, "Post", "DocumentHeader")

    Set oAccountPayable = oBAPICtrl.DimAs(oPostInvoice, "Post", "AccountPayable")

    Set oAccountGL = oBAPICtrl.DimAs(oPostInvoice, "Post", "AccountGL")

    Set oAccountTax = oBAPICtrl.DimAs(oPostInvoice, "Post", "AccountTax")

    Set oCurrencyAmount = oBAPICtrl.DimAs(oPostInvoice, "Post", "CurrencyAmount")

    Set oInvoiceReturn = oBAPICtrl.DimAs(oPostInvoice, "Post", "Return")

    Set oCommitReturn = oBAPICtrl.DimAs(oBAPIService, "TransactionCommit", "Return")

    End Sub

    Private Function PostInvoice(asCompany As String, asVendor As String, abBatch As Boolean, Optional aLog As Integer, Optional asLogFile As String = "")

    ' -


    ' MODULE: PostInvoice

    ' AUTHOR: Nick Fry

    ' NARRATIVE: Calls the BAPI functions to Check, Post and Commit the

    ' Invoice and then read back open items for the vendor to

    ' get the SAP document number.

    ' HISTORY: 11-Feb-2005 NF Created

    '

    ' OWNERS: B&Q PLC

    ' -


    '

    ' Parameters reqd : asCompany + asVendor - who this is posting to so we can retrieve line items

    Dim bCheckValid As Boolean

    Dim i As Long

    PostInvoice = False

    'Perform a validation check on the details set up first

    oPostInvoice.Check DocumentHeader:=oDocumentHeader, _

    AccountPayable:=oAccountPayable, _

    AccountGL:=oAccountGL, _

    AccountTax:=oAccountTax, _

    CurrencyAmount:=oCurrencyAmount, _

    Return:=oInvoiceReturn

    'Handle errors at the remote call

    bCheckValid = True

    If Not (oInvoiceReturn Is Nothing) Then

    gstrBAPIMessage = ""

    For i = 1 To oInvoiceReturn.rowCount

    If oInvoiceReturn.Value(i, "TYPE") <> "" And oInvoiceReturn.Value(i, "TYPE") <> "S" Then

    bCheckValid = False

    End If

    gstrBAPIMessage = gstrBAPIMessage + vbCRLF + "* " + oInvoiceReturn.Value(i, "TYPE") + oInvoiceReturn.Value(i, "NUMBER") + ": " + oInvoiceReturn.Value(i, "MESSAGE") + "(" + oInvoiceReturn.Value(i, "MESSAGE_V1") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V2") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V3") + ";" + oInvoiceReturn.Value(i, "MESSAGE_V4") + ")"

    Next

    End If

    If gstrBAPIMessage <> "" Then

    'gstrBAPIMessage = "Posting " & oDocumentHeader.Value("REF_DOC_NO") & "-" & oDocumentHeader.Value("DOC_DATE") & vbCRLF & gstrBAPIMessage

    If Not (asLogFile = "") Then Print #aLog, gstrBAPIMessage

    If Not abBatch And Not bCheckValid Then MsgBox gstrBAPIMessage, vbInformation, "Check Invoice"

    End If

    If bCheckValid Then

    ' Post the invoice

    oPostInvoice.Post DocumentHeader:=oDocumentHeader, _

    AccountPayable:=oAccountPayable, _

    AccountGL:=oAccountGL, _

    AccountTax:=oAccountTax, _

    CurrencyAmount:=oCurrencyAmount, _

    Return:=oInvoiceReturn

    ' Commit the post so that we can get the document number

    oBAPIService.TransactionCommit Wait:="X", _

    Return:=oCommitReturn

    ' Key the vendor statement object to the current vendor

    Set oVendorStatement = Nothing

    Set oVendorStatement = oBAPICtrl.GetSAPObject("APAccount", asCompany, asVendor)

    Set oLineItems = oBAPICtrl.DimAs(oVendorStatement, "GetOpenItems", "LineItems")

    Set oStmtReturn = oBAPICtrl.DimAs(oVendorStatement, "GetOpenItems", "Return")

    ' Retrieve a statement of open items for the vendor

    oVendorStatement.GetOpenItems KeyDate:=oDocumentHeader.Value("PSTNG_DATE"), _

    LineItems:=oLineItems, _

    Return:=oStmtReturn

    ' Loop through the items to match the one just posted

    For i = oLineItems.rowCount To 1 Step -1

    If UCase(oLineItems.Value(i, "ALLOC_NMBR")) = UCase(oDocumentHeader.Value("REF_DOC_NO")) Then

    mLastSAPDoc = oLineItems.Value(i, "DOC_NO")

    If Not (asLogFile = "") Then Print #aLog, "Document posted to " & mLastSAPDoc

    Exit For

    End If

    Next

    End If

    PostInvoice = True

    End Function

    Check this links

    http://abap4.tripod.com/SAP_and_VBA__Visual_Basic_for_Applications_.html

    http://www.suite101.com/discussion.cfm/vb_script/81157

    http://visualbasic.ittoolbox.com/groups/technical-functional/vb-access-l/extract-data-from-sap-using-visual-basic-965538

    Regards

    Pavan

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.