Showing results for 
Search instead for 
Did you mean: 

How can I open next Sap session when current is running using VBA

0 Kudos

Hello All

I have problem to find VBA code to open new Sap window when current session is running

I am working in SAP and for example running transaction and in this time I want to run macro with recorded Sap session

Maybe it explain better my problem

I need code which copy this action - transaction is running end on left top Sap screen I choosing “New GUI window” clicking and new window appears

It’s code which I use code waiting until running session is off and start new window

I am looking for code that open sew window immediately

Private Sub nextSession()

Dim nSessions As Integer

Set SapGuiAuto = GetObject("SAPGUI")

Set SAPApp = SapGuiAuto.GetScriptingEngine

Set sapCon = SAPApp.Children(0)

Set session = sapCon.Children(0)

nSessions = sapCon.Sessions.Count



Application.Wait (Now() + 500 * ms)

If sapCon.Sessions.Count > nSessions Then Exit Do


Set sapSession = sapCon.Sessions.Item(CInt(sapCon.Sessions.Count - 1))

End Sub

Community Manager
Community Manager
0 Kudos

Hi Grzegorz,

Thank you for visiting SAP Community to get answers to your questions. Since you're asking a question here for the first time, I recommend that you familiarize yourself with:, as it provides tips for preparing questions that draw responses from our members.

For example, you can:

- outline what steps you took to find answers (and why they weren't helpful)

- share screenshots of what you've seen/done

- make sure you've applied the appropriate tags

- use a more descriptive subject line

The more details you provide, the more likely it is that members will be able to respond. Feel free to also take our Q&A tutorial at:

Should you wish, you can revise your question by selecting Actions, then Edit.

By adding a picture to your profile you encourage readers to respond:



Accepted Solutions (0)

Answers (2)

Answers (2)

0 Kudos

Thank you Stefan

It works properly

Active Contributor
0 Kudos


Hello Grzegorz,

try the Busy method to check if you can use a session or not.

Best regards

 Set SapGuiAuto = GetObject("SAPGUI")
 If Not IsObject(SapGuiAuto) Then
   Exit Function
 End If

Set app = SapGuiAuto.GetScriptingEngine If Not IsObject(app) Then Exit Function End If
'-Get all connections------------------------------------------------- Set CollCon = app.Connections() If Not IsObject(CollCon) Then Exit Function End If
'-Loop over connections----------------------------------------------- For i = 0 To CollCon.Count() - 1 Set oCon = app.Children(CLng(i)) If Not IsObject(oCon) Then Exit Function End If
'-Get all sessions of a connection---------------------------------- Set CollSes = oCon.Sessions() If Not IsObject(CollSes) Then Exit Function End If
'-Loop over sessions------------------------------------------------ For j = 0 To CollSes.Count() - 1 Set oSes = oCon.Children(CLng(j)) If Not IsObject(oSes) Then Exit Function End If
If oSes.Busy() = vbFalse Then '-If you can use the session place your code here
End If