Just little about C#, .NET, SQL Server, SharePoint and SAP

Calling an SAP BAPI method using VB Script

Posted by zieglers on November 30, 2007

 BAPI Diagram

If you are in need of calling a BAPI method using VB Script, it may mean that probably you already searched for this, tried several VB Script samples that you found from the web, tried to modify them and couldn’t manage to work. That’s why you are reading this post now 🙂

There are a number of things to be done before you attemp to call a BAPI using vb script. First of all, you should have ‘SAP.BAPI.1’ in your registry already. This means that you’d better have latest SAP GUI installed on the machine you are gonna run your script.

Secondly, just having your method being RFC-Enabled if not enough. There should be a corresponding SAP Business Object for it, and you should be able to browse this object in BAPI Explorer (Transaction: BAPI). I won’t give the details of creating an SAP Business Object in this post now. I just wanna warn you that you shouldn’t try to create an instance of your RFC-Enabled method, instead you should get an instance of the related business object!

Here is the sample code


Option Explicit
Dim oBAPICtrl        ‘BAPI Control
Dim oConnection      ‘Connection object
Dim Logon            ‘Logon help variable
Dim oLoadPDF

Dim oArcid
Dim oIndir
Dim oReturn

oArcid = “XY”
oIndir = “C:\test”
‘Creating BAPI object
Set oBAPICtrl = CreateObject(“SAP.BAPI.1”)

‘Creating Connection object
Set oConnection = oBAPICtrl.Connection
oConnection.Client = “010”
oConnection.User = “YOURUSERNAME”
oConnection.Language = “EN”
oConnection.ApplicationServer = “yourserveraddress”
oConnection.Password = “YOURPASSWORD”
oConnection.SystemNumber = “00”
‘Performing a remote logon to the R/3 System
If oConnection.Logon(0, True) <> True Then    ‘Logon with dialog
      Set oConnection = Nothing
      Logon = False
      MsgBox “No access to R/3 System”, vbOKOnly, APPID
     Logon = True
End If
If Logon Then
 MsgBox “Logged on successfully…”
End If

If Err.Number <> 0 Then
      MsgBox “failed on oBAPICtrl.GetSAPObject(“ZZ_YOUR_BUSINESS_OBJECT_NAME”)…”
      MsgBox “SAP Object created successfully…”
End If
If oLoadPDF.Loadpdf(oArcid, oIndir, oGTDATA, oReturn) <> True Then    ‘Call LOADPDF function
 MsgBox “LOADPDF Function called successfully…”
 MsgBox “LOADPDF Function call failed”
End If

Set oLoadPDF = Nothing
Set oConnection = Nothing
Set oBAPICtrl = Nothing


In this LoadPdf function, i was just calling some frontend upload functions to upload pdf files to sap server and start some workflow. I left the signature of the method intentionally so that you can see that return variable is also included while calling the method.

Also, note that this code is logging on SAP system in silent mode –> oConnection.Logon(0, True)

Here is the link of a VB sample from SAP Help site: http://help.sap.com/saphelp_46c/helpdata/en/76/4a42f7f16d11d1ad15080009b0fb56/content.htm

Hope, this helps… 🙂



3 Responses to “Calling an SAP BAPI method using VB Script”

  1. mwa3aan said


    This is pretty interesting. I was hoping to show you something that I would love to get your opinion on if you dont mind. I mailed the info@navantis.com e-mail address with my contact information. My name is Adriaan van Wyk. Hope to talk to you soon.

    Regards, Adriaan

  2. scpswordpress said

    Simple and easy to understand.
    For self leaners like me, the topic is quiet useful and creates interest to learn more.
    I am now tryin to call a BAPI that requires parameters and get data written to a text file using vb script. Pl share if we have any example.

    Thank you so much.
    S Chandran P S

  3. Particular Car

    Calling an SAP BAPI method using VB Script

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: