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

Archive for November 30th, 2007

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… 🙂



Posted in IT Stuff, SAP | 3 Comments »