You are on page 1of 5

Silent Connection with SAPLOGON with RFC SDK Example | RFC | Excel | VBA

Pgina 1 de 5

flag

Silent Connection with SAPLOGON with RFC SDK Example | RFC | Excel | VBA
SAP RFC (Remote Function Call) allows an external program to connect to the SAP system to either read data or write data to the SAP system. The RFC SDK is available when you install the SAP GUI (Frontend) and specify a custom or full installation. The RFC SDK is a very versatile interface tool; it has a COM interface allowing it to be used with VBA (Visual Basic).

by klanguedoc 174 Followers

This tutorial will walk you through the process of connecting to a SAP system without user intervention, also known as, silent connection. I will use Excel for this example to return a list of sales items that were sold. Open the Visual Basic Editor in Excel. You might need to enable the Developer Tab in Excel if it is not visible. Search the documentation or check out Customize the ribbon under the Options. These are the steps that I will perform: 1. Add a reference to SAP 2. Create Script 3. Retrieve Data

Add a reference to SAP


Under Tools | References in the VBA Editor menu, scroll to you find the SAP objects Select SAP Data Provider (sapdattap.ocx) Select SAPGUIRessources (sapguirm.ocx)

Create Script
Under the Modules folder in the VBA Editor in the Project Navigator create a new module (Right click -> new module). I called my module SAP_data but you can give the module any name you want. In the new module create a new Sub, like SAP (see Figure 1).

http://klanguedoc.hubpages.com/hub/Silent-Connection-with-SAPLOGON-with-RFCSD... 23/08/2012

Silent Connection with SAPLOGON with RFC SDK Example | RFC | Excel | VBA

Pgina 2 de 5

Enter the code like in Figure 1. You have objects for the worksbook and worksheet; an object for the BAPI Control (oBapiCtrl) and the SAP Login Control (oBapiLogon). Also create an object variable for sales (oSalesOrder) and the sales items (oItems). Next as is standard practice, you will need to set (initialize) your variables. Once the objects are created, set the logon parameters as in the screenshot. Using the Silent connection requires more information can an user providing the details. In particular, you will need the Application Server address, user and password, the language and finally the system number otherwise you will get an error.

Figure 1: Logon Code for Silient Connection

Logon Procedure

You can attempt to logon by testing for a successful logon. Like the following code snippet, otherwise you can exit the routine gracefully. If oBapiCtrl.Connection.Logon(0, True) <> True Then MsgBox "not connected", vbInformation, "SAP Logon" Exit Sub End If

http://klanguedoc.hubpages.com/hub/Silent-Connection-with-SAPLOGON-with-RFCSD... 23/08/2012

Silent Connection with SAPLOGON with RFC SDK Example | RFC | Excel | VBA

Pgina 3 de 5

If the system cant logon, simply exit the sub or as in the example, notify the end user that we couldnt log onto the system. Otherwise, setup the Worksheet to receive data from SAP. In my example I will get sales orders from the SAP system.

Retrieving Data
In the following code snippet, I test to see if the BAPI Control is connected before setting the oSalesOrder object to avoid throwing an error. The rest is pretty much self-explanatory, you simply map the object properties to the cell address.

Figure 2: Retrieve SAP Business Object and retrieve associated data.

The last property that I retrieve is the number of items in the order oSheet.Cells(5, 5).Value = oSalesOrder.items.Count I will use this value to loop through the items in the order and retrieve the orders details, as the following code snippet demonstrates:

http://klanguedoc.hubpages.com/hub/Silent-Connection-with-SAPLOGON-with-RFCSD... 23/08/2012

Silent Connection with SAPLOGON with RFC SDK Example | RFC | Excel | VBA

Pgina 4 de 5

. For Each oItem In oSalesOrder.items oSheet.Cells(iIndex, 1).Value = oItem.SalesDocumentno oSheet.Cells(iIndex, 2).Value = oItem.itemNo . The next section contains the complete code that you can copy and paste into your Excel or VB project. Remember to change the logon values to those appropriate for your company or test environment.

Complete Code
Sub sap() Dim oBook As Workbook Dim oSheet As Worksheet Dim oBapiCtrl As Object Dim oBapiLogon As Object Dim oSalesOrder As Object Dim oCustomer As Object Dim oProduct As Object Dim oItem As Object Dim iIndex As Integer Set oBook = Application.ActiveWorkbook Set oSheet = oBook.Worksheets(1) Set oBapiCtrl = CreateObject("sap.bapi.1") Set oBapiLogon = CreateObject("sap.logoncontrol.1") oBapiCtrl.Connection = oBapiLogon.NewConnection oBapiCtrl.Connection.ApplicationServer = "000.000.000.000" oBapiCtrl.Connection.System = "test" oBapiCtrl.Connection.Client = "0000" oBapiCtrl.Connection.User = "someuser" oBapiCtrl.Connection.Password = "xxxxxxxxxx" oBapiCtrl.Connection.Language = "EN" oBapiCtrl.Connection.SystemNumber = "00" If oBapiCtrl.Connection.Logon(0, True) <> True Then MsgBox "not connected", vbInformation, "SAP Logon" Exit Sub End If If oBapiCtrl.Connection.IsConnected Then Set oSalesOrder = oBapiCtrl.GetSAPObject("SalesOrder", "0000000000") oSheet.Cells(4, 1).Value = "Sales Document" oSheet.Cells(4, 2).Value = "Net Value" oSheet.Cells(4, 3).Value = "Customer Number" oSheet.Cells(4, 4).Value = "Document Date" oSheet.Cells(4, 5).Value = "Number of Items" oSheet.Cells(5, oSheet.Cells(5, oSheet.Cells(5, oSheet.Cells(5, oSheet.Cells(5, End If iIndex = 8 Loop through the order item details oSheet.Cells(7, 1).Value = "Order No" oSheet.Cells(7, 2).Value = "Item No" oSheet.Cells(7, 3).Value = "Product Code" oSheet.Cells(7, 4).Value = "Product Description" oSheet.Cells(7, 5).Value = "Selling Quantity" 1).Value 2).Value 3).Value 4).Value 5).Value = = = = = oSalesOrder.salesdocument oSalesOrder.netvalue oSalesOrder.orderingparty.customerno oSalesOrder.documentdate oSalesOrder.items.Count
?

http://klanguedoc.hubpages.com/hub/Silent-Connection-with-SAPLOGON-with-RFCSD... 23/08/2012

Silent Connection with SAPLOGON with RFC SDK Example | RFC | Excel | VBA

Pgina 5 de 5

oSheet.Cells(7, 6).Value = "Selling UoM" oSheet.Cells(7, 7).Value = "Product Description" For Each oItem In oSalesOrder.items oSheet.Cells(iIndex, 1).Value = oSheet.Cells(iIndex, 2).Value = oSheet.Cells(iIndex, 3).Value = oSheet.Cells(iIndex, 4).Value = oSheet.Cells(iIndex, 5).Value = oSheet.Cells(iIndex, 6).Value = oSheet.Cells(iIndex, 7).Value = iIndex = iIndex + 1 Next oSheet.Columns.AutoFit Set oBapiLogon = Nothing Set oBapiCtrl = Nothing End Sub

oItem.SalesDocumentno oItem.itemNo oItem.material.material oItem.material.materialdescription oItem.targetquantity oItem.targetquantityunit oItem.Description

Caveat: In is critical to get acquainted with the BAPI Explorer, Transaction: BAPI, in SAP to find the Business Objects, the import parameters and fields and methods. Also you will find the corresponding BAPIs and you can run tests to check out what the function is returning as values. I hope the article was helpful and remember to get to know the BAPI Explorer. The answers are in there, except you will need to look for them.

This Hub was last updated on November 25, 2011

http://klanguedoc.hubpages.com/hub/Silent-Connection-with-SAPLOGON-with-RFCSD... 23/08/2012

You might also like