on 08-14-2014 2:20 PM
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.