Skip to Content
avatar image
Former Member

Sap Standard Screen Selection

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Oct 18, 2015 at 04:48 PM

    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

    Add comment
    10|10000 characters needed characters exceeded

    • Hello Robin,

      thanks for your reply.

      I checked it and I got the same result as you - after the second loop the tabs object is empty and the VBA program throws an error.

      Your work around is excellent 😊

      Thank you for that.

      Cheers

      Stefan

  • avatar image
    Former Member
    Oct 21, 2015 at 09:58 AM

    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
    
    Add comment
    10|10000 characters needed characters exceeded