cancel
Showing results for 
Search instead for 
Did you mean: 

Sap Standard Screen Selection

Former Member
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

stefan_schnell
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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

  1. '-Sub EmptyPushFields-------------------------------------------------
  2.     Sub EmptyPushFields(obj)
  3. '-Variables-------------------------------------------------------
  4.         Dim cntObj, i, Child, Field
  5. '---------------------Main----------------------------------------------------
  6.       On Error Resume Next
  7.       cntObj = obj.Children.Count()
  8.       If cntObj > 0 Then
  9.         For i = 0 To cntObj - 1
  10.           Set Child = obj.Children.Item(CLng(i))
  11.           EmptyPushFields Child
  12.             If InStr(Child.ID(), "/usr/") Then
  13. '------------------ Remove range Values -------------------------
  14.                If InStr(Child.ID(), "VALU_PUSH") Then
  15.                     If InStr(Child.tooltip, "Active") Then
  16.                         Sleep 100
  17.                         Set Field = Session.findById(Child.ID)
  18.                         Field.press
  19.                         Sleep 500
  20.                         Session.findById("wnd[1]/tbar[0]/btn[16]").press
  21.                         Sleep 500
  22.                         Session.findById("wnd[1]/tbar[0]/btn[8]").press
  23.                         Sleep 500
  24.                    End If
  25.                 End If
  26.             End If ' Error Occurs Here
  27.         Next
  28.       End If
  29. End Sub

Thanks again for your help with this

stefan_schnell
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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

stefan_schnell
Active Contributor
0 Kudos

Hello Robin,

thanks for your reply. I developed the code with SAP GUI version 7.40 PL 5 and 7.30 PL 14 and VBScript. In both cases the code works with TAC SE16 and the table TADIR. What TAC do you use?

Cheers

Stefan

Former Member
0 Kudos

Right, I think that is it, just tried the function in a few different transactions IA10 IW29 IH06, and it works fine, the TACs that i'm experiencing the issue with  are IW37n and CV04N which are all (Subscreens with tabs)

stefan_schnell
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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

  1. Q = 0
  2.    
  3.     For i = 0 To session.findById("wnd[0]/usr").Children.Count() - 1
  4. ReFindTabs:
  5.       Set Tabs = session.findById("wnd[0]/usr").Children.Item(CLng(i))
  6.       If Tabs.Type() = "GuiTabStrip" Then
  7.         For J = Q To Tabs.Children.Count() - 1
  8.           Tabs.Children.Item(CLng(J)).Select
  9.         EmptyTextFields session
  10.         Call ShellAndWait("cscript ""C:\Test\TabStrip.VBS""", 0, vbHide, PromptUser)
  11.         Q = Q + 1
  12.         GoTo ReFindTabs
  13.         Next
  14.       End If
  15.     Next

VBS Code File (Thanks to Stefan Schnell for the content)

  1. If Not IsObject(application) Then
  2.    Set SapGuiAuto  = GetObject("SAPGUI")
  3.    Set application = SapGuiAuto.GetScriptingEngine
  4. End If
  5. If Not IsObject(connection) Then
  6. Set connection = application.Children(SapApp.Children.Count - 1)
  7. End If
  8. If Not IsObject(session) Then
  9.    Set session    = application..Children(SapApp.Children.Count - 1).sessions(SapApp.Children(SapApp.Children.Count - 1).sessions.Count - 1)
  10. End If
  11. If IsObject(WScript) Then
  12.    WScript.ConnectObject session,     "on"
  13.    WScript.ConnectObject application, "on"
  14. End If
  15.         Dim cntObj, i, Child
  16.       On Error Resume Next  
  17.       cntObj = obj.Children.Count()  
  18.       If cntObj > 0 Then  
  19.         For i = 0 To cntObj - 1  
  20.           Set Child = obj.Children.Item(CLng(i))  
  21.           EmptyPushEntries Child  
  22.           If InStr(Child.ID(), "/usr/") Then  
  23.             If InStr(Child.ID(), "-VALU_PUSH") Then  
  24.               If Child.IconName() = "BGMORE" Then 
  25.                 Child.Press() 
  26.                 session.findById("wnd[1]").sendVKey 16 
  27.                 session.findById("wnd[1]").sendVKey 8 
  28.               End If 
  29.             End If  
  30.           End If  
  31.         Next  
  32.       End If  
  33.       On Error Goto 0  
  34.      End Sub  
stefan_schnell
Active Contributor
0 Kudos

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

Answers (1)

Answers (1)

Former Member
0 Kudos

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