Record macro in SAP using SAP Script Recording

I’ve already discussed how to Connect to SAP via Excel VBA and 10 code lines you can identify in SAP code. But I haven’t explained how to record a macro in SAP yet. In this article I’m going to explain You step by step how to record a macro in SAP using SAP Script Recording.

Record macro in SAP using SAP Script Recording Morfeus

I am going to show You how to start your first recording, beginning of the new era of your SAP work.

First of all…

You need to open SAP application and log in. After that, having that main SAP window opened, click on the last icon with 2 small wheel gears. From the expanded list please select Script Recording and Playback, like on the image below.

Record macro in SAP using SAP Script Recording options

When You select and click it, the pop up window will appear.

Record macro in SAP using SAP Script Recording record and playback

Please click More button to see more options.

Record macro in SAP using SAP Script Recording record and playback more

In the first place You can see 2 active buttons – Play and Record – and one not active – Stop. You are interested in the Record button. After clicking it every move in SAP will be recorded. When You are finished click Stop button to stop recording.

Here You can also see the directory of your recorded scripts. You can change it if You want to. And You don’t have to bother about the format in the window below the path.

After recording

After You finish your recording go to the place from the directory. There You will see files with the .vbs extension. To see the content of the file just simply right click it, select Open with and choose Notepad.

Record macro in SAP using SAP Script Recording script.vbs

After Notepad loads, the content for logging in will look like this.

If Not IsObject(application) Then
   Set SapGuiAuto  = GetObject("SAPGUI")
   Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
   Set connection = application.Children(0)
End If
If Not IsObject(session) Then
   Set session    = connection.Children(0)
End If
If IsObject(WScript) Then
   WScript.ConnectObject session,     "on"
   WScript.ConnectObject application, "on"
End If

session.findById("wnd[0]").resizeWorkingPane 175,37,false
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "user"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "********"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus
session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 10
session.findById("wnd[0]").sendVKey 0

In this case I recorded my attempt to log in to SAP.

Everything after conditionals is your clicks and keyboard types in SAP. You can exchange the first part of your code for the connection code from my Connect to SAP article.

And that’s it!

Now You should know how record macro in SAP using SAP Script Recording. You know where to find it, You know how to use it. It will automate your daily SAP routines and make your daily tasks faster and easier.

Author: Tomasz Płociński

I'm very advanced in VBA, Excel, also easily linking VBA with other Office applications (e.g. PowerPoint) and external applications (e.g. SAP). I take part also in RPA processes (WebQuery, DataCache, IBM Access Client Solutions) where I can also use my SQL basic skillset. I'm trying now to widen my knowledge into TypeScript/JavaScript direction.

33 thoughts on “Record macro in SAP using SAP Script Recording”

  1. hi thomasz
    I saw your post about login to sap with macro and it worked fine for me here but, I’m not allowed to record macros with scripting in my work I can just record the login window, after that nothing more appears in the code. seeing your blog I can adapt some code and execute a transaction but I can’t fill the textbox anyway after the transaction is executed. do you have any idea?

    I just added one line after the login code:
    session.findById(“wnd[0]/tbar[0]/okcd”).Text = “iw22”, that’s the transaction after that appears a textbox were I have to added a number..

    I can’t find a way to fill that textbox.

    thanks!!

    1. Hello Gabriel
      I can’t help You unfortunately, don’t have access to that transaction. It’s really weird that You can record login window, but nothing else. Besides, You should talk with someone in your company about this to be able to record scripts, what will solve all your problems!

    2. Hi Gabriel,

      You should check with your organization SAP governance team for the authorization profile to run script on that particular server.

      An SAP server administrator could enable or disable the scripting support at the server setting. If the scripting support is disabled, you cannot even get to the login screen with a script.

      If you can login using a script, that means it is enabled, but once logged in, the profile authorization check comes into force, and access to the scripting support is blocked.

      Contact your SAP administrator to get the authorization.

  2. Hi Thomasz!

    Thank you! This is very helpful.
    If I want to run a script but don’t want to close SAP everytime I want to run it, how would the code look like? I tried running the script directly from SAP without the log in code but it doesn’t work 🙁

    1. Hello Ingrid
      Just don’t close it, You can find it here
      Ps. You can run recorded scripts (having SAP already opened) just with double-click on it.

  3. Hi Tomas

    Is there a way to use that connection code only once , so it will be visible between modules ?
    Example:

    Sub test ()
    If Not IsObject(application) Then
    Set SapGuiAuto = GetObject(“SAPGUI”)
    Set application = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(connection) Then
    Set connection = application.Children(0)
    End If
    If Not IsObject(session) Then
    Set session = connection.Children(0)
    End If
    If IsObject(WScript) Then
    WScript.ConnectObject session, “on”
    WScript.ConnectObject application, “on”
    End If

    session.findById(“wnd[0]”).resizeWorkingPane 175,37,false
    session.findById(“wnd[0]/usr/txtRSYST-BNAME”).text = “user”
    session.findById(“wnd[0]/usr/pwdRSYST-BCODE”).text = “********”
    session.findById(“wnd[0]/usr/pwdRSYST-BCODE”).setFocus
    session.findById(“wnd[0]/usr/pwdRSYST-BCODE”).caretPosition = 10
    session.findById(“wnd[0]”).sendVKey 0

    call test2

    end sub

    sub test2 ()
    session.findById(“wnd[0]/tbar[0]/okcd”).text = “/nmd11”
    session.findById(“wnd[0]”).sendVKey 0
    end sub

    1. If You use “/n” before the transaction name You don’t have to logout / close SAP session, so You can connect just once.

      1. Hi

        But that code will give me following error message
        “Run-time error ‘424’ – object required in sub test2 .
        When il will changed code to following in sub test2 – but as You can see that “connection” code is used twice – is there a way to avoid that and use that code only once in whole module?

        Sub test2()
        If Not IsObject(fdddd) Then
        Set SapGuiAuto = GetObject(“SAPGUI”)
        Set fdddd = SapGuiAuto.GetScriptingEngine
        End If
        If Not IsObject(Connection) Then
        Set Connection = fdddd.Children(0)
        End If
        If Not IsObject(session) Then
        Set session = Connection.Children(0)
        End If
        If IsObject(WScript) Then
        WScript.ConnectObject session, “on”””
        WScript.ConnectObject fdddd, “on”””
        End If
        session.findById(“wnd[0]/tbar[0]/okcd”).Text = “/nmd11”
        session.findById(“wnd[0]”).sendVKey 0
        End Sub

        1. Hi Bartosz,
          You are getting the error, because You forgot to pass the session object into sub test2

          1. Read about passing variables/arguments into subroutines 🙂

  4. Hi Tomasz Płociński,

    Thanks for teaching us…

    I have a question on SAP Record Macro. I have recorded one macro which could help us to download the invoice from SAP-Document-Attachments through FB03 Screen.
    Recorded Macro helps me to download but I am unable to save that PDF into respective location in local drive.

    Can you please help me on this case

    1. Hello Parandham
      Have You changed the location path during the recording? Can You share here your recorded code or just the saving as part?

  5. Hello Champions,

    I have an excel file of M6 Notifications & want to automate PM06 order generations. Could you please let me know how to do this ?

    Note1: Half of them are associated with maintenance plans other half do not
    Note2: All notifications have Revision number to group all shutdown M6 Notifications

    1. Hello Rashid
      Please type here comments according to the topic. This is not a forum or request store.
      I can help You with problems, but You can’t request the whole code.
      Have You tried anything?

  6. Hi Tomasz,
    I have a question on SAP Record Macro. I have recorded one macro which could help us to set the user data.
    I foud the macro stopped when I choose data of dropdown list. how to record choose data of dropdown list?

    1. Hi Mina,
      It would be easier seeing any recorded code or at least tell me the example transaction code.
      What do You mean the macro stopped? There is no “id” for the dropdown list object in recording?

  7. Hej,

    co może być powodem nie zapisania się macro na dysku? czy do nagrywania makr trzeba mieć specjalne uprawnienia w SAP?

    1. Cześć,
      Spróbuj wyczyścić folder z skryptami i nagrać ponownie, będąc pewnym że naciskasz przycisk STOP.

      Hello
      Try to empty folder, where the vbs files are saved, then attempt to record again. Be sure that in the end of the recording You press STOP button.

  8. Hi Thomasz!
    How to activate “Script Recording and Playback”?
    In my case it canot be executed.

    1. Hello
      Actually I don’t understand – why You want to activate something, which cannot be executed anyway?
      Can You explain more?

  9. I actually have one thing i have been working on for a while that i can’t solve, i think it will be suitable in this section.

    I have got the recording to work fine. But i get everything in one sub routine.

    But if i for instance want to have a few different buttons in excel that does different things.
    I:e the Sap is opened, and i want to “call that window” from another sub and execute my other script with transaktions and what not.
    I just cant see how i can do that, any suggestions of how to get back to the SAP window from an other Sub routine?

Leave a Reply

Your email address will not be published. Required fields are marked *