SAP session.findbyid – 10 code lines you can identify

When You learn to record script in SAP, your report work become a lot easier and faster. But what do these recorded, SAP secret code lines after session.findbyid mean?

SAP session.findbyid - what is this?

I already posted about How to connect to SAP via Excel VBA, where I present my approach to SAP connection with Excel VBA. So now let’s go to the meaning of some ID’s, which can help You to modify or repair your recorded script.

When I first saw recorded SAP code I felt like in Matrix. Tons of lines, where I only recognize what I was manually filling in SAP during recording.

But after some time I realized what some of that secret code lines mean. Thanks to that I could modify my code without re-recording. It allows me to shortage my code erasing unnecessary lines or putting my own variables into SAP code.

I would like to cut that whole, long journey experience and speed it up for You. For better understanding what hides behind SAP session.findbyid.

SAP code lines and their descriptions

1. SAP main window.

session.findById("wnd[0]")

You may notice that the abbreviation wnd comes from word window. Number zero starts numbering SAP windows and not only them. You’ll find out more windows in your code like wnd[1] or wnd[2].

2.Minimize and maximize the SAP window.

session.findById("wnd[0]").iconify
session.findById("wnd[0]").maximize

You can hide SAP window (.iconify) not to see any action on main stage wnd[0] or maximize (.maximize) to fullscreen.

3.Confirmation

session.findById("wnd[0]").sendVKey 0

Above line stands for confirmation. In other words it is classic ENTER button. So every time You click it during recording You’ll get this line.

4.Transaction name

session.findById("wnd[0]/tbar[0]/okcd").Text = "transaction name"

This time let’s take a look at the structure. Firstly comes window and its index – wnd[0], then toolbar – tbar[0] and transaction/ commands window on it – okcd. There is also .Text, which means that You are filling this textbox. Here You can pass transaction name or string variable.

5.Buttons

session.findById("wnd[0]/usr/btn[1]").press

Here You can see in structure as second item – usr. I’m not sure about that, but it rather stands for userform. Next to it is button nr 1 – btn[1]. Every time You click button, that action will be recorded as .press.

6.Cursor position

session.findById("wnd[0]/usr/ctxt[73]").caretPosition = 4

If You have ever seen .caretPosition it means that your cursor is in position, in our case it is after 4th sign in that text container – ctxt. In most cases it is completely unnecessary line, so You can delete it to make your SAP code more transparent.

7.Focused

session.findById("wnd[0]/usr/ctxt[73]").SetFocus

This is the second unnecessary SAP code line in my opinion. It is responsible for selecting window or text. Also feel free to delete it and it won’t affect on anything.

8.Check box

session.findById("wnd[0]/usr/chk[2]").Selected = True

If You have any checkbox in your transaction, it will appear in code as chk and its .Selected status – in our case it is True.

9.”Red cross”

session.findById("wnd[0]/tbar[0]/btn[12]").press

I think You know what I mean by saying “Red cross” on your main toolbar. After couple of recordings I realized that code line stands for cancel button. Use it to go back in SAP.

10.Closing

session.findById("wnd[0]").Close
session.findById("wnd[1]/usr/btn[0]").press

In the end I got for you code line for closing SAP. With closing line comes also confirmation pop up, which is next window wnd[1] with yes button btn[0].

And this was it!

That was my 10 code lines, which often follow session.findbyid in SAP. Realizing what they mean and what they stands for allowed me to have better control over the code and to optimize it. I hope it will help as well.

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 “SAP session.findbyid – 10 code lines you can identify”

  1. Thanks for sharing!
    BTW, you can find any ID for current SAP window using SAP official utility GUI_ScreenReader

  2. How can I get ID of used buttons, text boxes etc. used in a particular transaction?
    I can’t record a macro in SAP (I’m not authorized to use this transaction). Is it somehow to detect these IDs?

    1. Try to record it in test SAP, where You should have access to all transactions.

  3. Thanks Tomasz, it is really helpful !
    I am able to use macro to open SAP right now, do you know the macro script to select the 2nd button of my “selection screen” in the translation table? Thank you.

      1. Thanks Tomasz, It is really very helpful to my project! Now I get my SAP report by one click on Macro

  4. Thank you Tomasz, great quick overview, very helpful. Keep going, its very inspirative!

  5. Hello, thank you for writing this blog post. I am trying to use the code to close and I get an error on the second confirmation window.

    line:

    session.findById(“wnd[1]/usr/btn[0]”).press

    Run-time error ‘619’:

    The control could not be found by id.

    Thank you for your help!

      1. Hello, thanks for sharing, this was very helpful!
        I need to close the session in SAP but unfortunately the commands for clossing are just showing the pop up window “unsaved data will be lost”, I don’t know if I am missing something or how can I make it works,
        Thank you very much for your help! SAP guru 🙂

    1. Very late but below is solution of the error –

      session.findById(“wnd[1]/usr/btnSPOP-OPTION1”).press

      btnSPOP-OPTION1 – is for Yes
      and
      btnSPOP-OPTION1 – is for No

  6. session.findById(“wnd[0]/usr/ctxtS_BUKRS-LOW”).text = “277”

    Hi, I need to run this Code in excel VBA.
    and value 277 is based on the excel cell Value in A2,A3,a4 …etc

    How can I do this.. Please help!

    Thanks in advance!

    1. Just simply instead of “277” put there variable responsible for the cell value, for example:
      session.findById(“wnd[0]/usr/ctxtS_BUKRS-LOW”).text = ThisWorkbook.Sheets(1).Range(“A1”).Value

  7. Just wanted to say thank you so much for this post, it’s exactly what I’ve been looking for, and has been a game changer for me today. Finally getting my reports automated!

  8. Hai Tomasz

    Hope you are doing good!

    Thanks for great work! your script helped me in many ways
    I have another query can i know how to copy the text from SAP

    For eg. ctrl+y we select text in sap gui screen i need to copy that value and paste in sheet

    1. Hi Ganesh
      Thanks! Hope You good too!
      To copy the value/text from SAP try to record its window/placeholder/checkbox ID and use its value/text property.

  9. Hallo Tomasz,
    deinen Blog über SAP-Scripting finde ich sehr interessant.
    Ich möche einige Abläufe im SAP automatisieren.
    Dazu muss ich Daten von einer Transaktion in andere übertragen.
    Leider habe ich es bisher nicht geschafft, von einer Transaction zur anderen und wieder zurück zu wechseln.
    Wenn ich aus einer Transaktion im Code über “/n…” einer ander Transaktion starte, wird die ursprüngliche Transaktion geschlossen und ich muss wieder von vorne starten.
    Starte ich die nächste Transaktion über “/o…” wird das Script in der “neuen” Transaktion nicht weiter ausgeführt.
    Gibt es eine Möglichkeit so ein Transaktionshopping zu automatisieren?

  10. Hi Tomasz,
    first of all, thanks for sharing all these helpful information – most of the times, it is exactly what I was looking for 🙂 #thumpsup
    I just started my first “project” on connecting to SAP via Excel. Of course, I read your post about how to connect to SAP and just copied the code. The login works fine, however, with the transaction I always receive the error “method FINDBYID of object ISAPSESSIONTARGET failed”. If I enter the code within the login window (wnd[0]), the string get passed to the bar, no errors (even it makes no sense at all to put it there).
    What am I missing, any ideas?

  11. Hi Tomasz,

    Good topic! much appreciated.
    Wanted to add that ” – usr. I’m not sure about that, but it rather stands for userform. ”
    according to SAP help appears to stand for guiUSeRarea .
    It instructs the visual basic ‘findbyid’ function that the control is to be found with the guiuserarea screen element.

    lots (perhaps too much 😉 of info on https://help.sap.com/viewer/b47d018c3b9b45e897faf66a6c0885a8/760.03/en-US/babdf65f4d0a4bd8b40f5ff132cb12fa.html

    Many thanks for your posts!

Leave a Reply

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