0

When I am trying to script an additional SAP GUI window with the code:

session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/oVF02"

and the following code is

session.findById("wnd[0]/usr/tabsTAB300/moretext").Text = ActiveWorkbook.Sheets("shData").Cells(3, 7)
session.findById("wnd[0]/usr/tabsTAB300/moretex").Text = "Text"
session.findById("wnd[1]").sendVKey 0

I tried to write 1 instead of 0 but it doesn't work. I just want to script the window that is opened and not the one where I am writing the VF02, is there any form in the script to position the opened window as the one which is gonna be scripted. I do not know how to do it.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
  • After "typing" `/oVF02` you must press Enter in the same window to run `/oVF02`, i.e. `session.findById("wnd[0]").sendVKey 0`. – Sandra Rossi Apr 28 '23 at 17:03

2 Answers2

1

I think you want to create a new session and use this session as well in your script. Unfortunately (or at least I do not know any better) SAPGUI scripting does not return the session object in case you create a new session either by using /OVF02or by using session.CreateSession.

I use the following function to create a new session and get access to the newly created session itself

Function createNewSession(ByRef guiSes As SAPFEWSELib.GuiSession) As SAPFEWSELib.GuiSession

    
    Dim guiCon As SAPFEWSELib.GuiConnection
    Set guiCon = guiSes.Parent

    Dim newSes As SAPFEWSELib.GuiSession

    If Not guiSes Is Nothing Then
        With guiSes
    
            Dim noSessions As Long
            noSessions = guiCon.Sessions.Count
            
            Dim sngSes As SAPFEWSELib.GuiSession
            
            ' Collect all IDs
            Dim dict As Dictionary
            Set dict = New Dictionary
            For Each sngSes In guiCon.Sessions
                dict.Add sngSes.ID, sngSes.ID
            Next
        
            guiSes.CreateSession
            ' Script needs to wait as the above line runs asynchronously
            ' and it takes some time to open a new session
             Application.Wait Now + TimeValue("00:00:05")
            
            ' Compare the old ID  to all existing ones. The one
            ' that is not contained in the dictionary is the new session
            For Each sngSes In guiCon.Sessions
                If Not dict.Exists(sngSes.ID) Then
                    Set newSes = sngSes
                End If
            Next
    
        End With
    
    End If
    
    Set createNewSession = newSes

End Function

You can test it with your code

    Dim newSession As SAPFEWSELib.GuiSession
    Set newSession = createNewSession(session)
    With newSession
       .StartTransaction "VF02"
       ' further code

   End With

This will open a new session and the variable newSession will point to that newly created session. Then it will run VF02

Further reading

Storax
  • 11,158
  • 3
  • 16
  • 33
0

You are confusing a session (GuiSession object) and a window (GuiMainWindow or GuiModalWindow object).

/o is to start a session. From Microsoft Windows perspective, a SAP GUI session is a "Windows window".

The window 0 is the main screen of a session, and windows 1 to 9 are popups (modals) stacked one above the other in the same session.

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48