cancel
Showing results for 
Search instead for 
Did you mean: 

VB SCript Not recognizing the open SAP session

Former Member
0 Kudos

Friends really need help here

this is the code i have

Sub StartTransaction(SapGuiAuto, Connection, session)

' Dim SapGuiAuto As Object

Dim i As Long

i = 1

' There may be bad entries in the ROT from previous crashes

While i < 10 And SapGuiAuto Is Nothing

   i = i + 1

   On Error Resume Next

   Set SapGuiAuto = GetObject("SAPGUI")

   On Error GoTo 0

Wend

If SapGuiAuto Is Nothing Then

   MsgBox "Could not connect to SAPlogon process. Did you start it?", vbOKOnly Or vbCritical

   End

End If

On Error Resume Next

Set SAPApplication = SapGuiAuto.GetScriptingEngine

Set SapGuiAuto = Nothing

On Error GoTo 0

If SAPApplication Is Nothing Then

   MsgBox "Could not access GuiApplication. Maybe Scripting is disabled?", vbOKOnly Or vbCritical

   End

End If

Set Connection = SAPApplication.Children(0)

Set session = Connection.Children(0)

End Sub

Even though i have multiple sap sessions open, it fails at

Set session = Connection.Children(0)


Any help is appreciated

Accepted Solutions (1)

Accepted Solutions (1)

holger_khn
Contributor
0 Kudos

Hello.

Below code list all available SAP sessions in a messagebox (EXCEL VBA). You Need to reference SAPFEWSE.OCX in VBA Editor. This can be found in your program Folder for SAPGUI.



Sub List_SAP_Sessions()

    Dim SapGuiAuto As Object
    Dim i As Integer
    Dim iSession As Long
    Dim SAP_APP As Object
    Dim Connection As SAPFEWSELib.GuiConnection
    Dim Session As SAPFEWSELib.GuiSession
    Dim strMSG As String
    Dim strSessions$


    i = 1
    ' There may be bad entries in the ROT from previous crashes
    While i < 10 And SapGuiAuto Is Nothing
       i = i + 1
       On Error Resume Next
       Set SapGuiAuto = GetObject("SAPGUI")
       On Error GoTo 0
    Wend
   
    If SapGuiAuto Is Nothing Then
        MsgBox "Could not connect to SAPlogon process. Did you start it?"
        Exit Sub
    End If
   
    On Error Resume Next
    Set SAP_APP = SapGuiAuto.GetScriptingEngine
    Set SapGuiAuto = Nothing
    On Error GoTo 0
   
    If SAP_APP Is Nothing Then
   
        MsgBox "Could not access GuiApplication. Maybe Scripting is disabled?"
        Exit Sub
       
    End If
   
    Set SapGuiAuto = Nothing
    iSession = 0
   
    For Each Connection In SAP_APP.Children
        If Not Connection.DisabledByServer Then
            For Each Session In Connection.Children
                If Session.Busy = False Then
                    iSession = iSession + 1
                    strMSG = strMSG & (Session.Info.SystemName & " (" & CStr(Session.Info.SessionNumber) & ") (" & Session.Info.Client & ") | User: " & Session.Info.User & " | Transaction: " & Session.Info.Transaction & " | Session ID: " & Session.ID) & vbCrLf & vbCrLf
                End If
            Next
        End If
    Next

strMSG = strMSG & vbCrLf & "Sessions counted: " & iSession

MsgBox strMSG, vbInformation + vbOKOnly, "Information"

End Sub

You can use a similar code to connect to a specific session by using Sesssion.ID. I have written an function for my EXCEL VBA template for this requirement:


Public Function objSession(strSessionID As String) As Object

    Dim SapGuiAuto As Object
    Dim SAP_APP As Object
    Dim i As Integer
    Dim bConnect As Boolean
    Dim Connection As SAPFEWSELib.GuiConnection
    Dim Session As SAPFEWSELib.GuiSession
   
    Set objSession = Nothing
       
    i = 1
    ' There may be bad entries in the ROT from previous crashes
    While i < 10 And SapGuiAuto Is Nothing
       i = i + 1
       On Error Resume Next
       Set SapGuiAuto = GetObject("SAPGUI")
       On Error GoTo 0
    Wend
   
    If SapGuiAuto Is Nothing Then
   
        MsgBox "Please start SAPlogon"
        Exit Function
       
    End If
   
    On Error Resume Next
    Set SAP_APP = SapGuiAuto.GetScriptingEngine
    Set SapGuiAuto = Nothing
    On Error GoTo 0
   
    If SAP_APP Is Nothing Then
   
        MsgBox "Scripting disabled"
        Exit Function
       
    End If
   
    Set SapGuiAuto = Nothing
   
    For Each Connection In SAP_APP.Children
       If Not Connection.DisabledByServer Then
          For Each Session In Connection.Children
            If Session.Busy = False Then
             If Session.ID = strSessionID Then
                Set objSession = Session
                locSessionHandle = Session.FindById("wnd[0]").Handle
                bConnect = True
                Exit For
             End If
            End If
          Next
       End If
      If bConnect = True Then Exit For
    Next

End Function

Hope this Support you to find root cause for your issue.

Best regards,

Holger

Former Member
0 Kudos

Holger i used the first code.

The script returned the following message:


" Sessions counted: 0 "

but I have 4 sessions open.

one thing i am connecting to SAP via VPN.  will this make a difference?

thanks for your help and prompt response.

Former Member
0 Kudos

Holger, the following statement returns a true value.

If Not Connection.DisabledByServer Then = true

what does that mean?

holger_khn
Contributor
0 Kudos

Hello.

Seems like Scripting is disabled in your System.

If you have Access check Parameter 'sapgui/user_scripting' in Transaction RZ11

Former Member
0 Kudos

Thanks a lot Holger.  It is turned off. problem solved

Answers (0)