0% found this document useful (0 votes)
437 views21 pages

SAP Connectivity With MS Excel SCN

The document describes connecting SAP data to Microsoft Excel. It allows users to access real-time SAP data within Excel by refreshing the spreadsheet with a single click. The connectivity eliminates error-prone copying and pasting of data from SAP to Excel. It provides benefits like enabling users to query and update SAP transactions from within Outlook and Office applications. The document provides an example of creating a function module in SAP that retrieves customer master data based on input from an Excel file. It also includes Visual Basic code for connecting Excel to SAP and sending/receiving data between the two systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
437 views21 pages

SAP Connectivity With MS Excel SCN

The document describes connecting SAP data to Microsoft Excel. It allows users to access real-time SAP data within Excel by refreshing the spreadsheet with a single click. The connectivity eliminates error-prone copying and pasting of data from SAP to Excel. It provides benefits like enabling users to query and update SAP transactions from within Outlook and Office applications. The document provides an example of creating a function module in SAP that retrieves customer master data based on input from an Excel file. It also includes Visual Basic code for connecting Excel to SAP and sending/receiving data between the two systems.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

SAP Connectivity with MS Excel

Data from SAP to MS Office Applications

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

In spite of large moneys in SAP, most business users continue to be unsatisfied


with it because data from SAP is not easily accessible in their favorite desktop
productivity tools -- Microsoft Excel, Microsoft Outlook and Microsoft Word.
Since Microsoft Office Applications are not connected to SAP, users are forced
to
Use error-prone cut-copy-paste to populate their Excel spreadsheets.
Switch screens from Outlook to gather data and take decisions as they
respond to e-mails.
Manually update data from Excel Spreadsheets to enterprise applications.

Benefits of Data from SAP to Microsoft Office Applications


SAP-MS Office Connectivity extends data from SAP to Microsoft Excel,
Microsoft Outlook and Microsoft Word using a SOA based Information Delivery
Server and Microsoft Office Add-ins, and provides the following benefits
Eliminates cut-and-paste based data collecting mechanism from SAP to
Microsoft Excel.
Enables users to get real-time data from SAP within Excel with a singleclick Refresh.
Enables users to query and update SAP transactions from within Outlook
screens.
Allows the users to update SAP from within Excel, Outlook and Word.
Avoids any data-integrity issues with copying and reduces compliance
issues.

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

Apply VB code to make connection with SAP.


Read the input data for customer master from the excel file sheet.
Fetch data from SAP using connection code from SAP.
Display output within excel sheet itself.
SAP Scenarios
Create a function module in ABAP that is Remote Enabled.
Create IMPORT, EXPORT and TABLES parameters as per requirement from
MS excel.
Write logic to display customer master details based on input from Excel
file.
Activate the function module.
WellA picture says thousand words. So, lets begin!

MS Excel User Interface


First of all, lets create MS excel file with customer master input. We shall create
simple selection screen for user input.

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

Just follow following steps for this:

Click MS office button and select Excel Options button from the menu.

You will see here a new tab named Developer

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

Enter function group and Short text.

Make sure your function module is Remote Enabled.

http://scn.sap.com/docs/DOC-31015

17/4/16 16:45
Pgina 7 de 21

Now lets create Tables parameters:

Here we have two Tables parameters


ET_KUNNR (Input) : Customer numbers pass in to the function module
ET_CUST_LIST (Output): Data will be fetched in function module and
display on the excel sheet.
Note: We have created a Z structure (ZNM_CUST_LIST) for the output
list. Here are the fields for the structure:

http://scn.sap.com/docs/DOC-31015

17/4/16 16:45
Pgina 8 de 21

We are all set with input and output structures.


Now, lets fetch the data. Here is the only coding part came up in SAP. Look at
below screen.

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

understand each of them.

Variable

Description

objBAPIControl

For creating object to access SAP functions.

objgetaddress

To make BAPI function call via objBAPIControl.

vLastRow

Last row of output list

vRows

Total number of records returned from the SAP

vcount_add

Variable for increment records

Index_add

Variable for next record (Index)

objaddress

Object for SAP table (for customer master output)

objkunnr

Object for SAP table (for customer master input)

Variable

Description

LogonControl

Logon control is to make login in SAP.

R3Connection

Ro make connection to SAP R/3 using logon control.

retcd

Return Code

SilentLogon

If True no popup will ask to enter SAP login details

Here are the properties for the Get Address button.

http://scn.sap.com/docs/DOC-31015

17/4/16 16:45
Pgina 11 de 21

Lets code when Get Address button is clicked.


Setup the local variables
Private Sub GetAddress_Click()
Now lets setup the connection with SAP R/3 using following code.

Lets do SAP Login here.

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.

Here, ZNM_GET_CUSTOMER_DETAILS is our function module created in SAP.


ET_KUNNR: Customer details input details
ET_CUST_LIST: Customer output details.

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

Public objaddress, objkunnr As SAPTableFactoryCtrl.Table


MODULE:
Sub GetAddress_click()
Dim retcd

As Boolean

Dim SilentLogon As Boolean


' Set Connection
Set LogonControl = CreateObject("SAP.LogonControl.1")
Set objBAPIControl = CreateObject("SAP.Functions")
Set R3Connection = LogonControl.NewConnection
R3Connection.Client = "700"
R3Connection.ApplicationServer = ""
R3Connection.Language = "EN"
R3Connection.User = ""
R3Connection.Password = ""
R3Connection.System = ""
R3Connection.SystemNumber = ""
R3Connection.UseSAPLogonIni = False
SilentLogon = False
retcd = R3Connection.Logon(0, SilentLogon)
If retcd <> True Then MsgBox "Logon failed": Exit Sub
objBAPIControl.Connection = R3Connection
Set objgetaddress = objBAPIControl.Add("ZNM_GET_EMPLOYEE_DETAILS")

http://scn.sap.com/docs/DOC-31015

17/4/16 16:45
Pgina 16 de 21

Set objkunnr = objgetaddress.Tables("ET_KUNNR")


Set objaddress = objgetaddress.Tables("ET_CUST_LIST")
Dim sht As Worksheet
Set sht = ThisWorkbook.ActiveSheet
' Changes for the Loop
'Dim int1 As Integer
'Do While sht.Cells(int1, 2).Value <> " "
' Changes for the Loop End
If sht.Cells(6, 2).Value <> " " Then
'If ThisWorkbook.ActiveSheet.Cells(6, "B").Value <> " " Then
'objkunnr.Rows.Add objkunnr.Value(1, "SIGN") =
ThisWorkbook.ActiveSheet.Cells(6, 2).Value
objkunnr.Rows.Add
objkunnr.Value(1, "SIGN") = sht.Cells(6, 2).Value
'objkunnr.Value(1, "OPTION") = ThisWorkbook.ActiveSheet.Cells(6, 3).Value
objkunnr.Value(1, "OPTION") = sht.Cells(6, 3).Value
'objkunnr.Value(1, "LOW") = ThisWorkbook.ActiveSheet.Cells(6, 4).Value
objkunnr.Value(1, "LOW") = sht.Cells(6, 4).Value
'objkunnr.Value(1, "HIGH") = ThisWorkbook.ActiveSheet.Cells(6, 5).Value
objkunnr.Value(1, "HIGH") = sht.Cells(6, 5).Value
'End If
End If

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
'

ActiveSheet.Cells(10, 12) = "BAPI Call is Successfull"

'

ActiveSheet.Cells(11, 12) = vcount_add & "rows are entered"

http://scn.sap.com/docs/DOC-31015

17/4/16 16:45
Pgina 18 de 21

sht.Cells(10, 12) = "BAPI Call is Successfull"


sht.Cells(11, 12) = vcount_add & " rows are entered"
End If
R3Connection.Logoff
End Sub
COMMON ERROR: User defined Type not defined

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.

2. References window will be opened as below.

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)

4. Select wdtaocxU.ocx, wdtfuncU.ocx, wdtlogU.ocx files from the


location.Add wdobapiU.ocx if you are needed.

http://scn.sap.com/docs/DOC-31015

17/4/16 16:45
Pgina 20 de 21

5. Make your references are selected. Press OK. And save.

6. Thats it.

http://scn.sap.com/docs/DOC-31015

17/4/16 16:45
Pgina 21 de 21

You might also like