on 10-18-2015 7:49 AM
I have a number of Scripts that edit the selection screen, currently I have created a selection screen variant for each of the transactions that can be loaded to ensure that all user options are cleared, however the system periodically deletes these variants and I have to create them again,
is there a way to clear the screen selection using Sap GUI Script or is there a way to specify the selection screen to use the system default variant?
Thanks,
Robin
Hello Robin,
welcome in the Scripting Language forum.
The following script deletes all entries of a selection screen:
'-Begin-----------------------------------------------------------------
'-Directives----------------------------------------------------------
Option Explicit
'-Global Variables----------------------------------------------------
Dim SapGuiAuto, application, connection, session
'-Sub EmptyTextFields------------------------------------------------
Sub EmptyTextFields(obj)
'-Variables-------------------------------------------------------
Dim cntObj, i, Child, Field
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))
EmptyTextFields Child
If InStr(Child.ID(), "/usr/") Then
If InStr(Child.ID(), "-LOW") Or InStr(Child.ID(), "-HIGH") Then
Set Field = Child.Parent().FindByID(Child.ID())
Field.Text = ""
End If
End If
Next
End If
On Error Goto 0
End Sub
'-Main----------------------------------------------------------------
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
EmptyTextFields session
'-End-------------------------------------------------------------------
It deletes all entries in fields which ID ends with an -LOW or -HIGH. In a selection screen you have in a normal case this scheme of ID names. If your selection screen follows another scheme, change the code in line 17.
Let us know your results and if this code has solved your problem.
Cheers
Stefan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the response, I needed to change Child.Parent().FindByID(Child.ID()) to session.findbyid(Child.ID) to get it to work but it does work well, i also needed to add a loop command to allow it to repeat the process for all of the screen tabs in the main window,
my follow up question to this is to remove any multiple line selections i will need to go into the sub menu and delete all items, It will do this for the first item however when the red End If (Line 31) occurs the sap session crashes and i'm left with a blank webpage, any thoughts on the matter would be greatly appreciated
Thanks again for your help with this
Hello Robin,
thanks for your reply, try this to delete all selections:
'-Begin-----------------------------------------------------------------
'-Directives----------------------------------------------------------
Option Explicit
'-Global Variables----------------------------------------------------
Dim SapGuiAuto, application, connection, session
'-Sub EmptyPushEntries------------------------------------------------
Sub EmptyPushEntries(obj)
'-Variables-------------------------------------------------------
Dim cntObj, i, Child, Field
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))
EmptyPushEntries Child
If InStr(Child.ID(), "/usr/") Then
If InStr(Child.ID(), "-VALU_PUSH") Then
If Child.IconName() = "BGMORE" Then
Child.Press()
session.findById("wnd[1]").sendVKey 16
session.findById("wnd[1]").sendVKey 8
End If
End If
End If
Next
End If
On Error Goto 0
End Sub
'-Main----------------------------------------------------------------
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
EmptyPushEntries session
'-End-------------------------------------------------------------------
Let us know your results.
Cheers
Stefan
Thanks the code you have provided is a lot neater than my version,
I'm Still having the SapGui crash when the change is made (crashes after line 19) i think there must be something else going on will have a play and see if i can figure it out,
for Reference using Sap Gui V 740 Final Release interacting via VBA Excel
Hello Robin,
please try this to delete all entries of a selction screen with a tabstrip:
'-Begin-----------------------------------------------------------------
'-Directives----------------------------------------------------------
Option Explicit
'-Global Variables----------------------------------------------------
Dim SapGuiAuto, application, connection, session
Dim i, j, tabs
'-Sub EmptyPushEntries------------------------------------------------
Sub EmptyPushEntries(obj)
'-Variables-------------------------------------------------------
Dim cntObj, i, Child
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))
EmptyPushEntries Child
If InStr(Child.ID(), "/usr/") Then
If InStr(Child.ID(), "-VALU_PUSH") Then
If Child.IconName() = "BGMORE" Then
Child.Press()
session.findById("wnd[1]").sendVKey 16
session.findById("wnd[1]").sendVKey 8
End If
End If
End If
Next
End If
On Error Goto 0
End Sub
'-Main----------------------------------------------------------------
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
For i = 0 To session.FindByID("wnd[0]/usr").Children.Count() - 1
Set tabs = session.FindByID("wnd[0]/usr").Children.Item(CLng(i))
If tabs.Type() = "GuiTabStrip" Then
For j = 0 To tabs.Children.Count() - 1
tabs.Children.Item(CLng(j)).Select()
EmptyPushEntries session
Next
End If
Next
'-End-------------------------------------------------------------------
From line 41 to 49 you see a loop which switches between the tabs and deletes all entries.
Let us know your results.
Cheers
Stefan
Thanks, i have used your code to develop a work around,
Problem:
VBA will crash the SapGui when interacting with certain items contained in a Tab Strip Sub Screen,
Solution:
VBS scripts do not cause this behavior use VBA to Write VBS Script file and launch the file using the ShellandWait function CPearson Shell And Wait
Section in VBA Code
VBS Code File (Thanks to Stefan Schnell for the content)
This is the final code I ended up with Thanks again to Stefan for his input
this code will also require the shell & wait function from CPearson (line 09)
Section in VBA Code to Call
For I = 0 To session.findById("wnd[0]/usr").Children.Count() - 1
ReFindTabs:
Set tabs = session.findById("wnd[0]/usr").Children.Item(CLng(I))
If tabs.Type() = "GuiTabStrip" Then
For J = Q To tabs.Children.Count() - 1
tabs.Children.Item(CLng(J)).Select
EmptyTextFields session
Call WriteVBS
Call ShellAndWait("cscript " & VBSFile & "", 0, vbHide, PromptUser)
Q = Q + 1
GoTo ReFindTabs
Next
End If
Next
Sub Empty Text Fields
'-Sub EmptyTextFields-------------------------------------------------
Sub EmptyTextFields(obj)
'-Variables-------------------------------------------------------
Dim cntObj, I, Child, Field
'---------------------Main----------------------------------------------------
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))
EmptyTextFields Child
If InStr(Child.ID(), "/usr/") Then
'-------------Clear Single Values (Quick)--------------------------------------
If InStr(Child.ID(), "-LOW") Or InStr(Child.ID(), "-HIGH") Then
Set Field = session.findById(Child.ID)
Field.Text = ""
End If
'End If
End If
Next
End If
End Sub
Module code to Create VBS File
Public VBSFile As String
Option Explicit
Sub WriteVBS()
Dim TabClear1 As String
Dim TabClear2 As String
Dim objFso As Object
Dim objFile As Object
Dim FLDR_Name As String
TabClear1 = "'-Begin----------------------------------------------------------------- " & vbCrLf & _
" '-Directives---------------------------------------------------------- " & vbCrLf & _
" Option Explicit " & vbCrLf & _
" '-Global Variables---------------------------------------------------- " & vbCrLf & _
" Dim SapGuiAuto, application, connection, session " & vbCrLf & _
" Dim i, j, tabs " & vbCrLf & _
" '-Sub EmptyPushEntries------------------------------------------------ " & vbCrLf & _
" Sub EmptyPushEntries(obj) " & vbCrLf & _
" '-Variables------------------------------------------------------- " & vbCrLf & _
" Dim cntObj, i, Child " & vbCrLf & _
" On Error Resume Next " & vbCrLf & _
" cntObj = obj.Children.Count() " & vbCrLf & _
" If cntObj > 0 Then " & vbCrLf & _
" For i = 0 To cntObj - 1 " & vbCrLf & _
" Set Child = obj.Children.Item(CLng(i)) " & vbCrLf & _
" EmptyPushEntries Child " & vbCrLf & _
" If InStr(Child.ID(), " & Chr(34) & "/usr/" & Chr(34) & ") Then " & vbCrLf & _
" If InStr(Child.ID(), " & Chr(34) & "-VALU_PUSH" & Chr(34) & ") Then " & vbCrLf & _
" If Child.IconName() = " & Chr(34) & "BGMORE" & Chr(34) & " Then " & vbCrLf & _
" Child.Press() " & vbCrLf & _
" session.findById(" & Chr(34) & "wnd[1]" & Chr(34) & ").sendVKey 16 " & vbCrLf & _
" session.findById(" & Chr(34) & "wnd[1]" & Chr(34) & ").sendVKey 8 " & vbCrLf
TabClear2 = " End If " & vbCrLf & _
" End If " & vbCrLf & _
" End If " & vbCrLf & _
" Next " & vbCrLf & _
" End If " & vbCrLf & _
" On Error Goto 0 " & vbCrLf & _
" End Sub " & vbCrLf & _
" '-Main---------------------------------------------------------------- " & vbCrLf & _
" If Not IsObject(application) Then " & vbCrLf & _
" Set SapGuiAuto = GetObject(" & Chr(34) & "SAPGUI" & Chr(34) & ") " & vbCrLf & _
" Set application = SapGuiAuto.GetScriptingEngine " & vbCrLf & _
" End If " & vbCrLf & _
" If Not IsObject(connection) Then " & vbCrLf & _
" Set connection = application.Children(application.children.count - 1) " & vbCrLf & _
" End If " & vbCrLf & _
" If Not IsObject(session) Then " & vbCrLf & _
" Set session = connection.sessions(application.Children(application.Children.Count - 1).sessions.Count - 1) " & vbCrLf & _
" End If " & vbCrLf & _
" EmptyPushEntries Session " & vbCrLf & _
"'-End------------------------------------------------------------------- "
FLDR_Name = Environ("APPDATA") & "\SapScripting\"
Set objFso = CreateObject("Scripting.FileSystemObject")
If Not objFso.FolderExists(FLDR_Name) Then
objFso.CreateFolder (FLDR_Name)
End If
Set objFso = Nothing
Set objFso = CreateObject("Scripting.FileSystemObject")
VBSFile = Environ("APPDATA") & "\SapScripting\Tabclear.VBS"
Set objFile = objFso.CreateTextFile(VBSFile, True)
objFile.Write TabClear1 & TabClear2
objFile.Close
End Sub
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
84 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.