SAP Connectivity With MS Excel SCN
SAP Connectivity With MS Excel SCN
Using the connection SAP with MS Office, users spreads data from SAP
application to right within Microsoft Office desktop applications, including Excel,
Outlook, Word and Power point.
With the SAP-MS Office connectivity, end-users can interact with SAP
transactions directly within their Excel spreadsheet, their Outlook e-mail screen,
and their Word documents and on their Power point presentations.
Here we have a simple application to connect SAP with MS-office tools Excel.
But before thatWhy? How? And Use!
Why Data from SAP to MS Office Applications
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 1 de 21
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 2 de 21
Who Benefits
Production and Logistics Managers who want to create Excel reports from
latest SAP data.
Managers who want to track projects and status automatically as they
receive status e-mails.
Analysts who want to combine data from SAP and other enterprise data
stores for budgeting purposes.
Example:
Here is the main part comes up. We shall take simple scenario for SAP with MS
Excel connectivity. So lets begin!!
Just before start, I would like to give little overview on what we are actually going
to do. So, here are the scenarios!!
Excel File Scenarios
Create a MS excel file for user input.
Create user interface in excel sheet to input data for the customer master
and output cells.
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 3 de 21
Wellfilling cell values are simple here but how to add buttons (Get Address
and Reset Output) here? This was also new in my case since new version of MS
office.
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 4 de 21
Click MS office button and select Excel Options button from the menu.
Click on Insert and then select Button from the form control. Later on you
can double click on the button and set its properties (Caption, Color, etc.)
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 5 de 21
Congratulations! Our input screen is finished here. Lets design our output
screen now.
I have created my output screen just after the input screen. You can create
anywhere in the excel file. Here terms anywhere descries to other worksheet
also.
The output screen is simple as you see. Nothing much to do here. I just simple
colored the cells for better look.
Great! Looks like we finished user interface here. (Did we miss
something...Naah for now!)
SAP Select Data
Since we are taking simple example to fetch detail from customer master, lets
create a function module for it.
Create a function module.
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 6 de 21
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 7 de 21
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 8 de 21
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 9 de 21
Here, we are selecting all the customer details from the KNA1 (customer master)
table from the input table (ET_KUNNR) from excel file and return to the table
(ET_CUST_LIST).
Now we have all the records in the table ET_CUST_LIST.
So, again congratulation friends! You have completed most of the things. The
remaining step now is to display the list in the excel file.
Lets now connect the SAP and MS Excel.
Go to the Developer tab and click on Visual Basic icon.
The VB editor will be opened and we are going to write the code for connection
of excel and SAP with sending and receiving data.
Here is the variable declaration list. These are the global variables. Lets
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 10 de 21
Variable
Description
objBAPIControl
objgetaddress
vLastRow
vRows
vcount_add
Index_add
objaddress
objkunnr
Variable
Description
LogonControl
R3Connection
retcd
Return Code
SilentLogon
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 11 de 21
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 12 de 21
Perfect!! We have made the connection with SAP R/3. Now we are able to send
and receive the data from MS Excel to SAP R/3 and vice versa.
But how to send my customer details and receive? Hmmm..we need to use
internal tables those are created in SAP function module. (Do you
remember?....NO?... checkout Tables parameters in function
ZNM_GET_CUSTOMER_DETAILS). So, lets do this.
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 13 de 21
We are reading here each cell from excel worksheet for input.
Here we have called the FM and passed the input details to process.
Result:
vcount_add returns total number of records from the SAP. And we have already
set the loop to display records in the cells.
Here, R3Connection.Logoff is to sign off from your SAP account.
Here is the output screen:
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 14 de 21
Wow!! We have output result. Good job!! But what is the use of another button
Reset Output here? Yesgood question.
The Reset Output button will clear all the data and messages from the screen.
Look at below code:
---------------------------------------------------------------------------------------------------------------------Now we have completed the entire example here. N joy.
Here is the entire code:
SHEET1:
Option Explicit
Private LogonControl As SAPLogonCtrl.SAPLogonControl
Private R3Connection As SAPLogonCtrl.Connection
Private TableFactory As SAPTableFactory
Public Functions As SAPFunctionsOCX.SAPFunctions
Dim objBAPIControl, objgetaddress As Object
Dim vLastRow, vRows As Integer
Dim vcount_add, index_add As Integer
Dim rng As Range
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 15 de 21
As Boolean
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 16 de 21
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 17 de 21
returnfunc = objgetaddress.call
If returnfunc = True Then
vcount_add = objaddress.Rows.Count
For index_add = 1 To vcount_add
vRows = 11 + index_add
sht.Cells(vRows, 2) = objaddress.Value(index_add, "KUNNR")
sht.Cells(vRows, 3) = objaddress.Value(index_add, "LAND1")
sht.Cells(vRows, 4) = objaddress.Value(index_add, "NAME1")
sht.Cells(vRows, 5) = objaddress.Value(index_add, "ORT01")
sht.Cells(vRows, 6) = objaddress.Value(index_add, "PSTLZ")
sht.Cells(vRows, 7) = objaddress.Value(index_add, "REGIO")
sht.Cells(vRows, 8) = objaddress.Value(index_add, "KTOKD")
sht.Cells(vRows, 9) = objaddress.Value(index_add, "TELF1")
sht.Cells(vRows, 10) = objaddress.Value(index_add, "TELFX")
Next index_add
End If
' If address not exist then Show error
If vcount_add = "" Then
sht.Cells(10, 11) = "Invalid Input"
Else
'
'
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 18 de 21
Solets try to remove the one. As our friends have posted solutions, here are
the steps to avoid the error.
1. Go to your VBA Project and Select Tool --> References.
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 19 de 21
3. Select Browse and select .OCX files for Active X control from below path.
(SAP GUI 7.3 already installed on my machine)
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 20 de 21
6. Thats it.
http://scn.sap.com/docs/DOC-31015
17/4/16 16:45
Pgina 21 de 21