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 /OVF02
or 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