cancel
Showing results for 
Search instead for 
Did you mean: 

Capturing the ID of the focused element.

Former Member
0 Kudos

Hello all this is the first time I have posting any question to any kind of forum so please excuse my ignorance of where to submit questions. This seemed like the best place for a VBScript question....

Ok so the company I work for uses SAP and has created several PDF step by step guides to help users through some processes.  I have been tasked with converting some of these guides into scripts with the goal of saving time, reducing errors, etc...

I have been following the same pattern for each that I have converted... Record script to a .vbs file, edit file, submit users to use.

My question is whether or not there is a way to somehow capture as a string the ID of the element that currently has focus.  I am trying to clean up the issue of parts not being fully or properly extended to other plants.  The issue is that when I enter a part using MM01 I do not always enter at the same tab (I select all of them before entering.)  My end goal is to have the script be able to determine which Material Master tab it is on when entering a part via MM01. 

To accomplish this I noticed that no matter which tab you enter on the Material description box has the focus which in the case of the purchasing tab is accessed via:


session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP09/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2301/ctxtMARC-EKGRP")

What I want is to grab the "wnd[0]/usr/tabsTABSPR1/tabpSP09/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2301/ctxtMARC-EKGRP" portion of that.  Then with a Left() and a Right() slice get the "tabpSP09" portion which would assist the script in orientating itself.

To do all of this I would need to be able to get the ID of the initially focused text box.  I do not even know if this is something that can be done but I figured it couldn't hurt to ask.

I apologize for my long-winded explanation I wanted to be specific just in case there may even be a better way to reach my desired ends.

Thank you in advance for any help that may come my way.

Accepted Solutions (1)

Accepted Solutions (1)

stefan_schnell
Active Contributor
0 Kudos
Hello Adam,
it is possible with the ID property of the GuiFocus property of the GuiMainWindow object:

ActID = Ses.ActiveWindow.GuiFocus.ID

Here a VBA example:

'-Begin-----------------------------------------------------------------
  Sub Test()

    '-Variables---------------------------------------------------------
      Dim SAPGUI As Object
      Dim App As SAPFEWSELib.GuiApplication
      Dim Con As SAPFEWSELib.GuiConnection
      Dim Ses As SAPFEWSELib.GuiSession
      Dim ActID As String
 
    Set SAPGUI = GetObject("SAPGUI")
    If Not IsObject(SAPGUI) Then
      Exit Sub
    End If
 
    Set App = SAPGUI.GetScriptingEngine()
    If Not IsObject(App) Then
      SAPGUI = Nothing
      Exit Sub
    End If
 
    Set Con = App.Children(0)
    If Not IsObject(Con) Then
      Set App = Nothing
      Set SAPGUI = Nothing
      Exit Sub
    End If
 
    Set Ses = Con.Sessions(0)
    If Not IsObject(Ses) Then
      Set Con = Nothing
      Set App = Nothing
      Set SAPGUI = Nothing
      Exit Sub
    End If

    ActID = Ses.ActiveWindow.GuiFocus.ID
    Debug.Print ActID
 
    Set Ses = Nothing
    Set Con = Nothing
    Set App = Nothing
    Set SAPGUI = Nothing

  End Sub
'-End-------------------------------------------------------------------

And a VBScript example:

'-Begin-----------------------------------------------------------------

  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

  ActID = session.ActiveWindow.GuiFocus.ID

  MsgBox ActID

'-End-------------------------------------------------------------------

Hope it helps, let us know.

Cheers
Stefan
Former Member
0 Kudos

This absolutely made my day!  I will be posting here a lot me thinks...  Sincerest thanks.

Answers (0)