Technical Articles
Tip: SAP GUI Scripting VBA Code Snippet to Detect all IDs of the UI Elements
In a few cases it is in business context not allowed to install additional analyze software or tools. So applications that are permissible and available must be used, e.g. like Microsoft Office applications. This makes things a bit more laborious, but not impossible.
Here a Visual Basic for Application (VBA) example to detect all IDs of the UI Elements of a session of the SAP GUI for Windows with SAP GUI Scripting. The result is written into a Microsoft Excel table.
The sub routine Start connects to the session and calls the recursive sub routine GetAll, which detects all IDs. The global variable gColl stores all IDs as an array of strings.
'-Begin-----------------------------------------------------------------
Option Explicit
Dim gColl() As String
Dim j As Integer
Sub GetAll(Obj As Object) '---------------------------------------------
'-
'- Recursively called sub routine to get the IDs of all UI elements
'-
'-----------------------------------------------------------------------
Dim cntObj As Integer
Dim i As Integer
Dim Child As Object
On Error Resume Next
cntObj = Obj.Children.Count()
If cntObj > 0 Then
For i = 0 To cntObj - 1
Set Child = Obj.Children.Item(CLng(i))
GetAll Child
ReDim Preserve gColl(j)
gColl(j) = CStr(Child.ID)
j = j + 1
Next
End If
On Error GoTo 0
End Sub
Sub Start() '-----------------------------------------------------------
'-
'- Sub routine to get all UI elements of the SAP GUI for Windows
'- with connection 0 and session 0
'-
'-----------------------------------------------------------------------
Dim SapGuiAuto As Object
Dim app As SAPFEWSELib.GuiApplication
Dim connection As SAPFEWSELib.GuiConnection
Dim session As SAPFEWSELib.GuiSession
Dim i As Integer
Set SapGuiAuto = GetObject("SAPGUI")
If Not IsObject(SapGuiAuto) Then
Exit Sub
End If
Set app = SapGuiAuto.GetScriptingEngine
If Not IsObject(app) Then
Exit Sub
End If
Set connection = app.Children(0)
If Not IsObject(connection) Then
Exit Sub
End If
If connection.DisabledByServer = True Then
Exit Sub
End If
Set session = connection.Children(0)
If Not IsObject(session) Then
Exit Sub
End If
If session.Info.IsLowSpeedConnection = True Then
Exit Sub
End If
GetAll session
For i = LBound(gColl) To UBound(gColl)
Cells(i + 1, 1) = gColl(i)
Next
End Sub
'-End-------------------------------------------------------------------
Here an example of a result:
With the access to all UI elements you can detect each information you need, e.g. like name, type, position, size, etc. On this way you can e.g. easily compare two versions of an UI, to find the differences.
Very useful code that I was searching for some time. Thanks for sharing.
Please tell me what are the references that I need to add in EXcel VBA window to get this code running?
Kannan NP
Hello Kannan,
you have only to reference to SAPFEWSE.OCX library.
Best regards
Stefan
Hi Stefan,
Very useful script - I made a couple of small changes to get the control Type, Name and Id in a columnar format.
Thanks for sharing Andrew
Hello Stefan,
am kind of new with SAP scripting. I used this script, but it say "End of statement expected". Can you please tell me how to run this script correctly
Thank you and appreciate you help
Hara
Hello Hara,
please check if you copy the code completely, with no additional characters and correct spaces. The "End of statement expected" means that the statement is syntactically complete, but an additional programming element follows the element that completes the statement.
Best regards
Stefan
very userful and I will try it tommorrow