Skip to Content
avatar image
Former Member

How to get the coordinates of selected field, at runtime

Hi,

I am trying to automate the SAP testing in my organization.
My objective is to download the PSA table details to aspreadsheet.
I have created a code that carries out this task, with the recorded scripts.

However, to make the code generic, the script should read the table name, and download its data.

I have a piece of code in my reocrded script, that records the sap screen coordinates.

session.findById(“wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell”).selectItem ” 709″, “COL1”
This is a static entry. Is it possible to get the position of selected field at runtime.

Any help will be much appreciated.

Thanks,
Annie

Add comment
10|10000 characters needed characters exceeded

  • Former Member Matt Fraser

    Hi Matt,

    I am developing a macro code that connects to SAP GUI and executes the recorded scripts.

    I couldn't find any related tags from the available ones.

    Not able to add user defined tags, as it is throwing me an error as' Error occured'.

    If possible, could you please add the tags following tags from your end: 'SAP GUI scripting', 'vbscript', macro

    Thanks,

    Annie

  • Normally you shouldn't receive an error editing your own question, but sometimes the site hiccups. Anyway, I've added "UI SAP GUI for Windows" as a secondary tag, and "vbscript" and "macro" as user tags (they don't exist as managed tags, nor is there one that I could find for SAP GUI scripting, unfortunately).

    It does appear you are getting some good responses, though.

  • Former Member

    Please find the code used below.

    Private Sub btnlogin_Click()
    '-----------------------Declarations-----------------------'
    Dim SAP As Object
    Dim conn As Object
    Dim SapGuiAuto As Object
    Dim SAPguiApp As SAPFEWSELib.GuiApplication
    Dim session As Object
    Dim Wscript As Object
    Dim ws1 As Worksheet
    '---------------------------------------------------------'
    Set ws1 = Sheets(1)
    
    '-------------Connection settings-------------------------'
    Set SAP = CreateObject("SAP.functions")
    SAP.connection.RfcWithDialog = True
    Set conn = SAP.connection
    conn.System = "SYSTEM"
    conn.client = "100"
    conn.user = ws1.Cells(5, 10).Value
    conn.Password = ws1.Cells(7, 10).Value
    conn.Language = "EN"
    '---------------------------------------------------------'
    If conn.logon(0, False) <> True Then
        MsgBox ("Logon not possible")
    
    
    Else
        'If Not IsObject(SAPguiApp) Then
        If SAPguiApp Is Nothing Then
             Set SapGuiAuto = GetObject("sapgui")
             Set SAPguiApp = CreateObject("Sapgui.ScriptingCtrl.1")
        End If
        
        If IsObject(conn) Then
             Set conn = SAPguiApp.OpenConnectionbyconnectionstring("CONNECTIONSTRING", True)
        End If
            
        If session Is Nothing Then
             Set session = conn.Children(0)
        End If
        
        If Not IsObject(Wscript) Then
             Wscript.ConnectObject session, "on"
             Wscript.ConnectObject App, "on"
        End If
    '----------------------Action code------------------------'
        With session
                session.findById("wnd[0]").maximize
                session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "USERNAME"
                session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "PASSWORD"
                session.findById("wnd[0]/usr/pwdRSYST-BCODE").SetFocus
                session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 8
                session.findById("wnd[0]").sendVKey 0
                session.findById("wnd[0]/tbar[0]/okcd").Text = "rsa1"
                session.findById("wnd[0]").sendVKey 0
                session.findById("wnd[0]/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").SelectItem "          8", "1"
                session.findById("wnd[0]/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").ensureVisibleHorizontalItem "          8", "1"
                session.findById("wnd[0]/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").topNode = "          2"
                session.findById("wnd[0]/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").clickLink "          8", "1"
                session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[0]/shell").pressButton "%AWB_TREE_SEARCH"
                session.findById("wnd[1]/usr/txtRSAWBN_S_DYNPRO_0500-SEARCH_TERM").Text = ws1.Cells(9, 10).Value
                session.findById("wnd[1]/usr/txtRSAWBN_S_DYNPRO_0500-SEARCH_TERM").caretPosition = 8
                session.findById("wnd[1]").sendVKey 0
                session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").SelectItem "       709", "COL1"
                session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").ensureVisibleHorizontalItem "       709", "COL1"
                session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").itemContextMenu "       709", "COL1"
                session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").selectContextMenuItem "MANAGE"
                session.findById("wnd[1]/usr/tblSAPLRSSMCTRL_2600").getAbsoluteRow(0).Selected = True
                session.findById("wnd[1]/usr/tblSAPLRSSMCTRL_2600/lblG_T_2600-VER_STATUS[0,0]").SetFocus
                session.findById("wnd[1]/usr/tblSAPLRSSMCTRL_2600/lblG_T_2600-VER_STATUS[0,0]").caretPosition = 0
                session.findById("wnd[1]/tbar[0]/btn[5]").press
                session.findById("wnd[2]/tbar[0]/btn[5]").press
                session.findById("wnd[2]/usr/txtG_RECORD_MAX").Text = ""
                session.findById("wnd[2]/tbar[0]/btn[0]").press
                session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").setCurrentCell -1, ""
                session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectAll
                session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
                session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItem "&XXL"
                session.findById("wnd[1]/tbar[0]/btn[0]").press
    
    
                MsgBox ("Data downloaded")
        End With
    '---------------------------------------------------------'
    End If
    End Sub
    
    
    
  • Get RSS Feed

3 Answers

  • Best Answer
    Jan 19, 2017 at 10:50 AM

    Hi Annie,

    my suggestion is as follows:

    . . .
    session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[0]/shell").pressButton "%AWB_TREE_SEARCH"
    session.findById("wnd[1]/usr/txtRSAWBN_S_DYNPRO_0500-SEARCH_TERM").Text = ws1.Cells(9, 10).Value
    'The following command is not absolutely necessary and could lead to an error if the length of the parameter to be searched is less than 8.
    'session.findById("wnd[1]/usr/txtRSAWBN_S_DYNPRO_0500-SEARCH_TERM").caretPosition = 8
    session.findById("wnd[1]").sendVKey 0
    
    myKey = session.findById("wnd[0]/usr/cntlCNTL_CONTAINER/shellcont/shell/shellcont[0]/shell/shellcont[1]/shell[1]").getFocusedNodeKey()
    
    session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").SelectItem myKey, "COL1"
    session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").ensureVisibleHorizontalItem myKey, "COL1"
    session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").itemContextMenu myKey, "COL1"
    session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell").selectContextMenuItem "MANAGE"
    . . .
    

    Regards,

    ScriptMan

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Scriptman,

      Thanks for the response. I tried the way you suggested.

      But it is throwing me the "Object required" error, in the highlighted line.

      I assume that, this is probably because, mykey is not being assigned any value.

      This is how the selection appears, while running the script.

      As Stefan mentioned, seems like the node is selected, but not focused.

      Is there any method that fetches the selected node value, to mykey?

      Thanks and regards,

      Annie

      error.png (52.4 kB)
      error1.png (18.2 kB)
      bw.png (10.7 kB)
  • Jan 23, 2017 at 09:48 AM

    Hi Annie,

    the variable myKey expects a string and not Object.

    myKey = session.findById("wnd[0]/usr/cntlCNTL_CONTAINER/shellcont/shell/shellcont[0]/shell/shellcont[1]/shell[1]").getFocusedNodeKey()

    without command set at the beginning... ;-)

    And if it still does not work, you could use the following command.

    for example:

    session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[0]/shell").pressButton "%AWB_TREE_SEARCH"
    session.findById("wnd[0]/usr/cntlCNTL_CONTAINER/shellcont/shell/shellcont[0]/shell/shellcont[1]/shell[1]").setFocus()
    . . .
    

    Regards,
    ScriptMan

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      yaaay!!! it worked perfectly :)

      Thanks Scriptman. I had declared the mykey variable as object. Changed it, and i got what i wanted.

      Thanks a ton :)

  • Jan 19, 2017 at 10:22 AM

    Hello Annie,

    yes it is possible to get the selected items (nodes).

    Dim Nodes As SAPFEWSELib.GuiCollection
    Set Tree = session.findById("wnd[0]/shellcont[1]/shell/shellcont[0]/shell/shellcont[1]/shell/shellcont[1]/shell")
    Set Nodes = Tree.GetSelectedNodes
    

    With GetSelectedNodes you get all selected nodes, because with the GuiTree of TAC RSA1 is it possible to select multiple items.

    Here you see my three selected nodes.

    Let us know your results.

    Cheers
    Stefan

    P.S. I think you can find useful information about trees here.

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Stefan Schnell

      Thanks a lot Stefan.

      Scriptman's code helped me achieve, what exactly I wanted. So marked it as the answer.

      Will definitely try the method you suggested. Thanks a lot for the knowledge shared.

      Regards,

      Annie