8447779800, Low rate Call girls in Saket Delhi NCR
SAP Integration With Excel - Advanced Guide
1. 1
SAP integration with Excel -
Advanced Guide
Summary
This document is a follow-up of ‘SAP integration with Excel - Basic Guide’. It documents how Excel can be
made to interface with SAP and perform data posting activities.
Perquisites:
Basic SAP ABAP knowledge is required. SAP terminologies and jargons are applied for brevity.
Review ‘SAP integration with Excel - Basic Guide, by Benedict Yong’, as it is the part 1 of this two-part
series.
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.
2. SAP Excel Integration
2
Table of Contents
Business Requirements ........................................................................................................................... 3
System Architecture ................................................................................................................................ 5
Preparatory work in RFC FM.................................................................................................................... 6
Preparatory work in Excel VBA............................................................................................................... 12
Integration & Testing ............................................................................................................................. 14
Conclusion............................................................................................................................................ 16
Appendix .............................................................................................................................................. 17
Author Bio............................................................................................................................................. 23
Reference............................................................................................................................................. 24
3. SAP Excel Integration
3
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 would 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 Excel Sales Order Creation screen, with Sales Order information that will be posted.
Step 2: This is the Excel Screen after successful posting into SAP. SAP Sales Order number will be shown.
5. SAP Excel Integration
5
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. (This is also applied in the Basic Guide)
3-Tier Model-View-Controller Framework
In the 3-Tier MVC Framework, there are
View/ Interface:The role focuseson userinteraction;collectinganddisplayinginformation.Inour
specificcase,thiswill be ourExcel anditsembeddedVBA.
Controller:The role focusesontransmittingandmanipulationof information.Inourspecificcase,
thiswill be ourSAPRFC FunctionModule.
Model:The role focusesondata storage and itsrelatedprocesses. Inourspecificcase,thiswillbe
the underlyingDatabase.
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.
6. SAP Excel Integration
6
Preparatory work in RFC FM
The purpose of RFC FM/BAPI is to populate sales header and line items data into SAP backend.
This can be achieved by creating a wrapper RFC FM as “ZZZ_SO_BAPI_CREATE_N”. This RFC FM will
perform appropriate coordination and relay information to/from the standard BAPI
“BAPI_SALESORDER_CREATEFROMDAT2” & “BAPI_TRANSACTION_COMMIT”. The standard BAPI
“BAPI_SALESORDER_CREATEFROMDAT2” will generate a new sales order number after all checking is done;
while the “BAPI_TRANSACTION_COMMIT” actually post the document. In the case, where BAPI
“BAPI_SALESORDER_CREATEFROMDAT2” is ran but “BAPI_TRANSACTION_COMMIT” is not, the sales order
number will be exhausted.
7. SAP Excel Integration
7
At the specific mapping level, it is required for us to understand what are the fields required from the frontend
(i.e. Excel) and what are the variables to transfer to at the backend (i.e. Standard BAPI).
8. SAP Excel Integration
8
The wrapper RFC FM is designed to take in minimal data from its incoming interfaces (i.e. Excel VBA). This
is a good practice. The incoming parameters (imports) will take in flat type structures instead of table types.
The flat type structures include sales header data (i.e. SO_HEADER of type ZZSOHEADER) and line items
data (i.e. SO_ITEM1 and SO_ITEM2 of type ZSSOITEM). Once successfully posted, the system generated
Sales Order number will be relay back to the call interface.
Note: full code at appendix.
9. SAP Excel Integration
9
Definition of sales header data (i.e. SO_HEADER of type ZZSOHEADER)
Definition of line items data (i.e. SO_ITEM1 and SO_ITEM2 of type ZSSOITEM)
Note: there is more than one way to implement RFC Sales Order Creation. The above is one of the
possibilities. It should also be noted that Table objects transferring can be cumbersome between SAP and
VBA (it might be better with JAVA/C++/C#), to be safe we have chosen to transfer line items as flat type
structure instead of collection type table (i.e. in the TABLE parameter | the IMPORT paramater for Table-type
Structure).
10. SAP Excel Integration
10
Function Module Unit Testing is as per expectation. A Sales Order header and two line items information is
entered, and Sales Order number 13308 is generated.
Processing messages are as below:
12. SAP Excel Integration
12
Preparatory work in Excel VBA
To be able to perform scripting in Excel VBA, the Developer Tab needs to be turned on. (This is documented
in the Basic Guide)
The start of the script is to instantiate ActiveX Components “SAP LogonControl.1” and “SAP.Functions” as
per below.
13. SAP Excel Integration
13
The actual function call to “ZZZ_SO_BAPI_CREATE_N” is as per below.
Note: full script at appendix.
14. SAP Excel Integration
14
Integration & Testing
With both the Excel VBA scripting and SAP RFC FM coding completed, we can perform end-to-end testing.
We start with data entry to the customer code cell (=D6), customer reference cell (=D7), line item 1 row
(=C10:G10) and line item 2 row (=C11:G11).
Note that Sales Order number cell (=D4) will be updated by the system upon successful update.
Once the ‘Submit SO’ button is pressed, the Excel VBA will make connection with SAP RFC FM via the
ActiveX components. The SAP RFC FM (custom), will populate appropriate information to various structures
required by the standard BAPI, and invoke the BAPI. The SAP RFC FM finally relay the generated Sales
Order number back to the VBA. The VBA projects the returned information in the Sales Order number cell
(=D4), with a message box notification. Noting the Sales Order number is #13309.
16. SAP Excel Integration
16
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.
Based on this two-part series, we can observe the feasibility of writing and retrieving SAP information using
intuitive interfaces – which can then be further scaled into enterprise-level. However, one needs to be
mindful, integration always takes two parts to work: one part SAP; one part third-party.
17. SAP Excel Integration
17
Appendix
Full RFC FM ABAP
FUNCTION zzz_so_bapi_create_n.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(SO_HEADER) TYPE ZSSOHEADER
*" VALUE(SO_ITEMS1) TYPE ZSSOITEM
*" VALUE(SO_ITEMS2) TYPE ZSSOITEM
*" EXPORTING
*" VALUE(SD_DOC_NUMBER) LIKE BAPIVBELN-VBELN
*" VALUE(E_INFO) TYPE CHAR256
*" TABLES
*" RETURN STRUCTURE BAPIRET2 OPTIONAL
*"----------------------------------------------------------------------
DATA l_order_header_in LIKE bapisdhd1.
DATA t_order_partners TYPE STANDARD TABLE OF bapiparnr.
DATA t_order_items_in TYPE STANDARD TABLE OF bapisditm.
DATA t_order_schedules_in TYPE STANDARD TABLE OF bapischdl.
DATA t_order_schedules_inx TYPE STANDARD TABLE OF bapischdlx.
PERFORM add_header TABLES t_order_partners
CHANGING l_order_header_in
so_header.
PERFORM add_items TABLES t_order_items_in
t_order_schedules_in
t_order_schedules_inx
CHANGING so_items1.
PERFORM add_items TABLES t_order_items_in
t_order_schedules_in
t_order_schedules_inx
CHANGING so_items2.
CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
EXPORTING
order_header_in = l_order_header_in
IMPORTING
salesdocument = sd_doc_number
TABLES
return = return
order_items_in = t_order_items_in
order_partners = t_order_partners
order_schedules_in = t_order_schedules_in
order_schedules_inx = t_order_schedules_inx.
PERFORM return_op TABLES return
CHANGING sd_doc_number
so_header-purch_no_c
e_info.
IF e_info+0(2) = 'S:'.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
ENDIF.
ENDFUNCTION.
18. SAP Excel Integration
18
*----------------------------------------------------------------------*
***INCLUDE LZZZ_MDF01 .
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form ADD_ITEMS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM add_items TABLES t_order_items_in STRUCTURE bapisditm
t_order_schedules_in STRUCTURE bapischdl
t_order_schedules_inx STRUCTURE bapischdlx
CHANGING so_item TYPE zssoitem.
CHECK so_item IS NOT INITIAL.
DATA l_order_items_in LIKE bapisditm.
DATA l_order_schedules_in LIKE bapischdl.
DATA l_order_schedules_inx LIKE bapischdlx.
l_order_items_in-itm_number = so_item-itm_number.
IF so_item-item_categ IS NOT INITIAL.
l_order_items_in-item_categ = so_item-item_categ.
ENDIF.
l_order_items_in-target_qty = so_item-quantity.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = so_item-material
IMPORTING
output = l_order_items_in-material.
l_order_items_in-plant = so_item-plant.
l_order_items_in-short_text = so_item-short_text.
APPEND l_order_items_in TO t_order_items_in.
l_order_schedules_in-itm_number = so_item-itm_number.
l_order_schedules_in-sched_line = '0001'.
l_order_schedules_in-req_qty = so_item-quantity.
APPEND l_order_schedules_in TO t_order_schedules_in.
l_order_schedules_inx-itm_number = so_item-itm_number.
l_order_schedules_inx-sched_line = so_item-sched_line.
l_order_schedules_inx-updateflag = 'X'.
l_order_schedules_inx-req_qty = 'X'.
APPEND l_order_schedules_inx TO t_order_schedules_inx.
ENDFORM. " ADD_ITEMS
*&---------------------------------------------------------------------*
*& Form ADD_HEADER
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_T_ORDER_PARTNERS text
* <--P_L_ORDER_HEADER_IN text
* -->P_SO_HEADER text
*----------------------------------------------------------------------*
FORM add_header TABLES t_order_partners STRUCTURE bapiparnr
19. SAP Excel Integration
19
CHANGING l_order_header_in TYPE bapisdhd1
so_header TYPE zssoheader.
DATA l_order_partners LIKE bapiparnr.
MOVE-CORRESPONDING so_header TO l_order_header_in.
l_order_partners-partn_numb = so_header-partn_numb_sp.
l_order_partners-partn_role = 'AG'.
APPEND l_order_partners TO t_order_partners.
IF so_header-partn_numb_sh IS NOT INITIAL.
l_order_partners-partn_numb = so_header-partn_numb_sh.
l_order_partners-partn_role = 'WE'.
APPEND l_order_partners TO t_order_partners.
ENDIF.
ENDFORM. " ADD_HEADER
*&---------------------------------------------------------------------*
*& Form RETURN_OP
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_RETURN text
* <--P_SD_DOC_NUMBER text
* <--P_E_INFO text
*----------------------------------------------------------------------*
FORM return_op TABLES return STRUCTURE bapiret2
CHANGING sd_doc_number
sd_po_number
e_info.
SHIFT sd_doc_number LEFT DELETING LEADING '0'.
DATA is_error TYPE boolean VALUE 0.
LOOP AT return.
IF return-type = 'E'.
is_error = 1.
CONCATENATE 'E:-' return-message
' (' sd_po_number ') '
INTO e_info.
EXIT.
ENDIF.
ENDLOOP.
IF is_error = 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
CONCATENATE 'S:-Sales Order (' sd_doc_number ')'
' Created Successfully'
INTO e_info.
ENDIF.
ENDFORM. " RETURN_OP
20. SAP Excel Integration
20
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
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
'---------------------------------
' Prepare a sales order.
'---------------------------------
Dim SO_Header As Object
21. SAP Excel Integration
21
Dim SO_Item1 As Object
Dim SO_Item2 As Object
Dim SO_Number As Object
Dim SO_Info As Object
Dim vRow, vCol As Integer
Set oBAPI = objBAPIControl.Add("ZZZ_SO_BAPI_CREATE_N")
Set SO_Header = oBAPI.Exports.Item("SO_HEADER")
Set SO_Item1 = oBAPI.Exports.Item("SO_ITEMS1")
Set SO_Item2 = oBAPI.Exports.Item("SO_ITEMS2")
Set SO_Number = oBAPI.Imports("SD_DOC_NUMBER")
Set SO_Info = oBAPI.Imports("E_INFO")
SO_Header.Value("DOC_TYPE") = "OR"
SO_Header.Value("SALES_ORG") = "3090"
SO_Header.Value("DISTR_CHAN") = "01"
SO_Header.Value("DIVISION") = "01"
SO_Header.Value("PURCH_NO_C") = Trim(oST.Cells(7, 4))
SO_Header.Value("PARTN_NUMB_SP") = Trim(oST.Cells(6, 4))
vRow = 10: vCol = 3
SO_Item1.Value("ITM_NUMBER") = Trim(oST.Cells(vRow, vCol + 0))
SO_Item1.Value("MATERIAL") = Trim(oST.Cells(vRow, vCol + 1))
SO_Item1.Value("QUANTITY") = Trim(oST.Cells(vRow, vCol + 2))
SO_Item1.Value("PLANT") = Trim(oST.Cells(vRow, vCol + 3))
SO_Item1.Value("SHORT_TEXT") = Trim(oST.Cells(vRow, vCol + 4))
vRow = 11: vCol = 3
SO_Item2.Value("ITM_NUMBER") = Trim(oST.Cells(vRow, vCol + 0))
SO_Item2.Value("MATERIAL") = Trim(oST.Cells(vRow, vCol + 1))
SO_Item2.Value("QUANTITY") = Trim(oST.Cells(vRow, vCol + 2))
SO_Item2.Value("PLANT") = Trim(oST.Cells(vRow, vCol + 3))
SO_Item2.Value("SHORT_TEXT") = Trim(oST.Cells(vRow, vCol + 4))
'---------------------------------
' Make the sales order.
'---------------------------------
Application.StatusBar = "Perform SAP Call"
SY_Subrc = oBAPI.Call
If SY_Subrc <> True Then MsgBox "Call failed!!": Exit Sub
If Left(SO_Info, 2) = "E:" Then MsgBox "Call failed: " & SO_Info: Exit Sub
MsgBox SO_Info
Application.StatusBar = "Perform SAP Call Successful"
Cells(4, 4).Value = SO_Number
22. SAP Excel Integration
22
Cells(4, 4).Interior.Color = vbGreen
'-----------------------------------
' Logoff SAP and close the control.
'-----------------------------------
R3Connection.Logoff
Set LogonControl = Nothing
Set objBAPIControl = Nothing
End Sub
23. SAP Excel Integration
23
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 benytx@gmail.com.
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
24. SAP Excel Integration
24
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