How to connect Excel to SAP


Hi,

if there is someone from you guys that work in SAP in their every day activity, and the tasks are repetitive, basic clicking and completing fields with text input, I would like to propose to you a way to automate this.
Firstly you need to make sure that the server administrator has given you the right to use scripting in your sap environmente. Log in to SAP, open SAP GUI Options->Accessibility & Scripting ->Enable scripting. This option needs to be checked, if it's not check it yourself. If the option is greyed out, unfortunately you won't be able to use sap scripting unless the administrator of the server changes this setting. (Most of them disable it by default because they think this might affect the system security, altough I think it's load of cra&)
Go to SAP main screen with the favorite transactions and select this icon (uppe right)then select ->Script Recording and Playback.
We can click the record button, and while it's recording we can enter a transaction, click a field, or a checkbox, or write something down into a field, and then exit the transaction. Stop the recording and look at the result! It's incredible, and the potential is almost limitless.
Click "More" from the recording window and check for the place where the script is saved. We go to that path, open the file (probably called Script1 or something like that) with notepad. You can see a very intuitive description of what you've done before stopping the script (Entered transaction IW32 for example, and completed a field with a character, did not save and exited). An example of action from my application:
Session.findById("wnd[0]/usr/txtS_ID_FIELD_REQ").Text = ActiveWorkbook.Sheets("Contor").Range("C" & i) 'I told SAP to reffrence the field for searching a request to be imported from my excel sheet. So if I write something down in C2 in Sheet1 I would get that data inserted in the field "ID_FIELD_REQ" automatically. (you can notice that I have "C&i"and that's because I implemented a for function that would loop trough all excel rows repeating that transaction over and over again until all data was inserted and submitted.

Session.findById("wnd[0]/usr/GENERAL_FLD").Text = "ABC Invoice 23112300312"
 An example of brute input. This is how you would tell SAP that field has a fixed value.

We create a new module (if you don't know how take a look at this article and we insert the code below.

Code:
Function Attach() As Boolean
On Error Resume Next
Set SapGuiAuto = GetObject("SAPGUI")
On Error GoTo 0
If SapGuiAuto Is Nothing Then
   Attach = False
   Exit Function
   Else
   Set Applicat = SapGuiAuto.GetScriptingEngine
   On Error GoTo 0
End If
If Applicat Is Nothing Then
   Attach = False
   Exit Function
End If
If Applicat.Children.Count = 0 Then
  Attach = False
  Exit Function
  Else
  Set Connection = Applicat.Children(0)
  On Error GoTo 0
End If
Set Session = Connection.Children(0)
If Session.ActiveWindow.Text = "SAP" Then
   Attach = False
   Exit Function
End If
Attach = True
End Function

This code establishes the connection between SAP and Excel VBA. You can insert the code from your notepad file below this code like this:

Sub Tranzactie()
****Notepad script****
End Sub

Once you try to run the script by selecting the play button from the module in VBA you will be greeted by a warning that's telling you some script is trying to attach to SAP, ok or cancel. Click ok and take a "look at the future old man!"

If you need help or collaboration contact me in the commnets below or privately.:-)

Comments