Search code examples
excelvbafocussap-gui

Find out which field in SAP has focus / is the active field with VBA (Excel)


I write scripts in VBA to change data via SAP GUI. I can easily set focus to a defined field in SAP GUI (e.g. Session.FindById("wnd[0]/usr/ctxt[2]").SetFocus). But how can I find out what field the focus is in SAP GUI? Does anyone know the code?

I already searched via google and also asked chatGPT, but all code I found did not work... I need a code that checks if the focus is on my field: if hasfocus(Session.FindById("wnd[0]/usr/ctxt[2]") then...) If the field is the active field it should be true otherwise false. Another solution would be to check where the focus is and compare it with my field address.

Does anyone know a possibility to check this?


Solution

  • Does this help

    Set Focus = session.ActiveWindow().systemFocus()
    if Focus.Id =  "wnd[0]/usr/ctxt[2]" then
       MsgBox "Got Focus"
    end if