cancel
Showing results for 
Search instead for 
Did you mean: 

VBA Script for running Sessions

abhi9945
Discoverer
0 Kudos

Hi All,

I have a VBA Script as below:

Sub OpenFEBANTtansaction()

Dim sapGuiApp As Object

Dim connection As Object

Dim session As Object

Set sapGuiApp = CreateObject("SAPGUI.ScriptingCtrl.1")

If sapGuiApp Is Nothing Then

MsgBox "SAP GUI Scripting is not available."

Exit Sub

End If

Set connection = sapGuiApp.OpenConnection("Production - S4HANA", True)

If connection Is Nothing Then

MsgBox "Connection to SAP failed."

Exit Sub

End If

If connection.Children.Count > 0 Then

Set session = connection.Children(0)

Else

MsgBox "No open sessions found."

End If

If session Is Nothing Then

MsgBox "Session not found."

Exit Sub

End If

session.StartTransaction "FEBAN"

Application.Wait (Now + TimeValue("0:00:02"))

session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "FEBAN"

session.findById("wnd[0]").sendVKey 0

End Sub

Here "Set session = connection.Children(0)" is not running and iam getting an error as "Run time error 614"Please help me to fix it

Accepted Solutions (0)

Answers (2)

Answers (2)

abhi9945
Discoverer
0 Kudos

HI Bohdan,


Thank you very much ! Your code worked well for me and the issue is resolved

Really Appreciated!

Bohdan
Active Contributor
0 Kudos

Hi abhi9945,

What is the purpose of the GUI script ? What are you trying to do with these few lines of the code ? Normally... well at least that's the way how I approach it some time ago - around 2018, when I was trying to run any VBA-script associated with SAP, I was already logged to the target system & client. My script was just trying to find an existing open connection and based on that do something i.e. in my case for example I had several utilities to update various current settings for IC transaction e.g. OBCA, OBCE, OBCB etc. But the point was - I was already logged in the system. Are you logged when you try to establish the connection ?

Here is the code that I was using to establish the connection. Original version was copied from some YouTube video, but I refactored it quite a lot. Maybe it will give you some ideas on the resolution of your problem.

Option Explicit

' SAP global variables
Public SapGuiAuto, WScript
Public objGui  As GuiApplication
Public objConnection As GuiConnection
Public objSession As GuiSession
Public objStatusBar As GuiStatusbar

' Excel variables
Dim g_wb As Workbook
Dim g_ws As Worksheet
Dim g_SAP_System As String

Function attach_gui_session() As Boolean
    Dim i, j
    Dim strConnection, strSession
    
    ' Reference to system is stored in cell B2 in format (NNNYYY where NNN is SID and YYY client)
g_SAP_System = ActiveSheet.Cells(2, 2) ' If we are already connected to a session, exit do not try again If g_SAP_System = "" Then attach_gui_session = False Exit Function End If ' If the session object is not nil, use that session (assume connected to the correct session) If Not objSession Is Nothing Then If objSession.Info.SystemName & objSession.Info.Client = g_SAP_System Then attach_gui_session = True Exit Function End If End If ' If not connected to anything, set up the objects If objGui Is Nothing Then Set SapGuiAuto = GetObject("SAPGUI") Set objGui = SapGuiAuto.GetScriptingEngine End If ' Cycle through the open SAP GUI sessions and check which is in the same system running the matching transaction For i = 0 To objGui.Children.Count - 1 Set strConnection = objGui.Children(i + 0) For j = 0 To strConnection.Children.Count - 1 Set strSession = strConnection.Children(j + 0) If strSession.Info.SystemName & strSession.Info.Client = g_SAP_System Then Set objConnection = objGui.Children(i + 0) Set objSession = objConnection.Children(j + 0) Exit For End If Next Next ' If nothing is found, display and error message If objSession Is Nothing Then MsgBox "No active session to system " + g_SAP_System + ", or scripting is not enabled.", vbCritical + vbOKOnly attach_gui_session = False Exit Function End If ' Turn on scripting If IsObject(WScript) Then WScript.ConnectObject objSession, "on" WScript.ConnectObject objGui, "on" End If ' Maximize the window of the connected session Set objStatusBar = objSession.FindById("wnd[0]/sbar") objSession.FindById("wnd[0]").Maximize attach_gui_session = True End Function