100% found this document useful (2 votes)
564 views

SAP Integration With Excel - Basic Guide

Excel VBA with SAP - a dream come true?

Uploaded by

Yong Benedict
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
564 views

SAP Integration With Excel - Basic Guide

Excel VBA with SAP - a dream come true?

Uploaded by

Yong Benedict
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 19

SAP integration with Excel -

Basic Guide

Summary
This document provides a foundation overview of SAP Integration with Excel. This is a two-part series
document: the basic guide shows how Excel can retrieve data from SAP; the advanced guide shows how
Excel can post data into SAP.

Perquisites:
Basic SAP ABAP knowledge is required. SAP terminologies and jargons are applied for brevity.

Disclaimers:
Although the author and publisher have made every reasonable attempt to achieve complete accuracy of the
content, they assume no responsibility for errors or omissions. You should use this information as you see fit,
and at your own risk.
This publication is not affiliated with, sponsored by, or approved by SAP. Any trademarks, service marks,
product names or named features are assumed to be the property of their respective owners, and are used
only for reference.

1
SAP Excel Integration

Table of Contents
Business Requirements.................................................................................................................................... 3
System Architecture.......................................................................................................................................... 5
Preparatory work in RFC FM............................................................................................................................ 6
Preparatory work in Excel VBA......................................................................................................................... 8
Integration & Testing....................................................................................................................................... 13
Conclusion...................................................................................................................................................... 14
Appendix......................................................................................................................................................... 15
Author Bio....................................................................................................................................................... 18
Reference....................................................................................................................................................... 19

2
SAP Excel Integration

Business Requirements
In an enterprise with SAP, there can be business units where sales order creation is minimal. Hence, there is
a requirement to create a streamlined user entry/retrieval interface.
It will be a dream come true to have sales order data input in Excel and have these data posted into SAP at a
click of a button and review them back again in Excel. In this document, we will go through how to create a
SAP RFC Function Module that can be called by an Excel VBA.

Base on the above, we will design a simple proof of concept.


Step 1: This is the Sales Order in SAP screen that will be retrieved into Excel format.

Step 2: This is the Excel Sales Order Retrieval screen, to be populated with Sales Order information.

3
SAP Excel Integration

Step 3: This is the Excel Screen after Sales Order information has retrieved from SAP.

4
SAP Excel Integration

System Architecture
As the Excel Integration process comprises of various components and interactions, a 3-Tier Model-View-
Controller Framework should be applied to manage the complexity.

3-Tier Model-View-Controller Framework

In the 3-Tier MVC Framework, there are


 View / Interface: The role focuses on user interaction; collecting and displaying information. In our
specific case, this will be our Excel and its embedded VBA.
 Controller: The role focuses on transmitting and manipulation of information. In our specific case,
this will be our SAP RFC Function Module.
 Model: The role focuses on data storage and its related processes. In our specific case, this will be
the underlying Database.

This document will now be divided into 2 sections:


 Preparatory work in RFC FM – this explains how the Controller of the architecture is coded.
 Preparatory work in Excel VBA – this explains how the View/Interface of the architecture is
scripted.

5
SAP Excel Integration

Preparatory work in RFC FM


The purpose of RFC FM/BAPI is to read sales header and line items data from SAP backend.

This can be achieved by creating a wrapper RFC FM as “ZZZ_SO_BAPI”. This RFC FM will perform
appropriate coordination and relay information to/from the standard BAPI
“BAPI_SALESORDER_GETDETAILBOS”. (It is a good practice to create a wrapper RFC FM, instead of having
the invoking interface calling the standard BAPI directly)

Note: the FM must be “Remote-Enabled”

Note: full code at appendix.

6
SAP Excel Integration

Function Module Unit Testing is as per expectation.


Sales Order 401004151 is retrieved with its header information and 3 line items.

7
SAP Excel Integration

Preparatory work in Excel VBA


To be able to perform scripting in Excel VBA, the Developer Tab needs to be turned on.

Once turned on, user interface controls such as buttons, checkbox, text field can be used.

8
SAP Excel Integration

This is the layout we will be using for Sales Order Retrieval. User will input the Sales Order to be retrieved
and press the “Retrieve SO” button. Information such as customer, customer reference PO# and the line
items will be displayed.

9
SAP Excel Integration

To attach VBA codes to the button, we need to access the “Assign Macro” context menu item.

Once selected, the VBA editor will be accessible.

10
SAP Excel Integration

SAP ActiveX components needs to be referenced by MS VBA, via the Tools menu.

Typically the ActiveX files (.OCX) are found in SAP frontend folder.

11
SAP Excel Integration

The start of the script is to instantiate ActiveX Components “SAP LogonControl.1” and “SAP.Functions”.

The actual function call to “ZZZ_SO_BAPI” is as per below.


Note: full script at appendix.

12
SAP Excel Integration

Integration & Testing


With both the Excel VBA scripting and SAP RFC FM coding completed, we can perform end-to-end testing.

Testing within SAP end Testing at Excel end

13
SAP Excel Integration

Conclusion
The standard way of access SAP is via SAP GUI. However, it is technically possible to access SAP using
ActiveX control delivered by SAP. This greatly enriches the developer toolset to provide user a wide array of
connectivity options (such as Excel VBA, JAVA, C++, ASP/C#, JavaScript). From a business perspective, an
intuitive user interface greatly enhances user experiences and potentially reduces user training cost.

Kindly look out for ‘SAP integration with Excel - Advanced Guide, by Benedict Yong’ for part II (last part) of
this two-part series.

14
SAP Excel Integration

Appendix
Full RFC FM ABAP
FUNCTION zzz_so_bapi.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(SALESDOCUMENT) LIKE BAPIVBELN-VBELN
*" EXPORTING
*" VALUE(ORDERHEADER) LIKE BAPISDHD STRUCTURE BAPISDHD
*" TABLES
*" ORDERITEMS STRUCTURE BAPISDITBOS OPTIONAL
*"----------------------------------------------------------------------

CALL FUNCTION 'BAPI_SALESORDER_GETDETAILBOS'


EXPORTING
salesdocument = salesdocument
IMPORTING
orderheader = orderheader
TABLES
orderitems = orderitems.

SELECT SINGLE name1 FROM kna1 INTO orderheader-sold_to


WHERE kunnr = orderheader-sold_to.

ENDFUNCTION.

15
SAP Excel Integration

Full Excel VBA Scripting


Sub Button1_Click()
'---------------------------------
' Declaration.
'---------------------------------
Dim LogonControl As SAPLogonCtrl.SAPLogonControl
Dim R3Connection As SAPLogonCtrl.Connection
Dim TableFactory As SAPTableFactory
Dim Functions As SAPFunctionsOCX.SAPFunctions
Dim objBAPIControl As Object
Dim oWB As Workbook
Dim oST As Worksheet
Set oWB = Application.ActiveWorkbook
Set oST = oWB.Worksheets(1)
'---------------------------------
' Initialize SAP ActiveX Control.
'---------------------------------
Set LogonControl = CreateObject("SAP.LogonControl.1")
Set R3Connection = LogonControl.NewConnection
Dim objBAPIControl As Object
Set objBAPIControl = CreateObject("SAP.Functions")
'--------------------
' Logon with prompt.
'--------------------
R3Connection.Client = "100" 'as per SAP Logon Pad
R3Connection.System = "SID" 'as per SAP Logon Pad
R3Connection.SystemNumber = "00" 'as per SAP Logon Pad
R3Connection.ApplicationServer = "XX.XX.XX.XX" 'as per SAP Logon Pad
R3Connection.User = "sapidx" 'as per SAP Logon Pad
R3Connection.Password = "sapid-passwdx" 'as per SAP Logon Pad
Application.StatusBar = "Start of Logging in"
SY_Subrc = R3Connection.Logon(0, SilentLogon)
If SY_Subrc <> True Then MsgBox "Logon failed": Exit Sub
Application.StatusBar = "Login Successful"
Set objBAPIControl.Connection = R3Connection
'---------------------------------
' Retrieve a sales order.
'---------------------------------

16
SAP Excel Integration

Dim vRow, vCol As Integer


Dim vStr As Integer
Dim vMax, vIdx As Integer
Dim oSearch As String
Dim oInfo As Object
Dim oTabl As Object
oSearch = Cells(4, 4).Value
Set oBAPI = objBAPIControl.Add("ZZZ_SO_BAPI")
oBAPI.Exports("SALESDOCUMENT") = oSearch
Set oInfo = oBAPI.Imports("ORDERHEADER")
Set oTabl = oBAPI.Tables("ORDERITEMS")
Application.StatusBar = "Perform SAP Call"
SY_Subrc = oBAPI.Call
If SY_Subrc <> True Then MsgBox "Call failed": Exit Sub
Application.StatusBar = "Perform SAP Call Successful"
oST.Cells(6, 4).Value = oInfo.Value("SOLD_TO")
oST.Cells(7, 4).Value = Trim(oInfo.Value("PURCH_NO"))
vMax = oTabl.Rows.Count
vCol = 3: vStr = 9
For vIdx = 1 To vMax
vRow = vStr + vIdx
oST.Cells(vRow, vCol + 0) = oTabl.Value(vIdx, "ITM_NUMBER")
oST.Cells(vRow, vCol + 1) = oTabl.Value(vIdx, "MATERIAL")
oST.Cells(vRow, vCol + 2) = Trim(oTabl.Value(vIdx, "NET_VALUE"))
oST.Cells(vRow, vCol + 3) = Trim(oTabl.Value(vIdx, "SHORT_TEXT"))
Next vIdx
'-----------------------------------
' Logoff SAP and close the control.
'-----------------------------------
R3Connection.Logoff
Set LogonControl = Nothing
Set objBAPIControl = Nothing
End Sub

17
SAP Excel Integration

Author Bio
Benedict Yong is a PMP/ITIL trained Project Consultant with 9+ years Finance domain experience (FICO,
COPA, BPC) and 3+ years of Logistics experiences (SD, MM, PS, CS). He holds four SAP® Functional
Certifications (Financial Accounting, Management Accounting, Sales, Procurement) and three Technical
Certifications (S/4 HANA Implementation Architect, S/4 Cloud Onboarding with SAP Activate, SAP
Business Intelligence 7.0).

He holds a Bachelor of Management and a Diploma in IT. He has worked in Banking,


Retail and Manufacturing industries, playing both in-house and external consultant
role.

He is situated in Singapore and is bilingual in English and Mandarin. He


can be contacted at [email protected].

For people who are interested to have a holistic understanding of ERP, a PDF document will not be
enough. “ERP Made Simple” at Amazon might prove to be useful.
https://www.amazon.com/dp/B083C3X8YY

18
SAP Excel Integration

Reference
1. SAP Help - BAPI Framework
https://help.sap.com/doc/saphelp_46c/4.6C/en-
US/d8/44ca02ac3c11d189c60000e829fbbd/content.htm

2. SAP OSS – note 2256415 - Adaptation of RFC controls (Logon, Function, Table and BAPI) to use
SAP NetWeaver RFC Library
https://launchpad.support.sap.com/#/notes/2256415

3. SAP SDN – Common export parameter issues


https://blogs.sap.com/2014/04/27/activex-component-sapfunctions-with-export-parameter-string/
https://answers.sap.com/questions/529288/datatype-problem-with-sap-gui-75-pl5-unicode-activ.html
https://answers.sap.com/questions/10222185/activex-component-sapfunctions-with-export-
paramet.html

19

You might also like