Hello Amit,
please try this to solve your requirement:
Private Declare Function BringWindowToTop Lib "user32" (ByVal _
hwnd As Long) As Long
Function cmdattachx() As Object
session_nr = -1
Set GuiAuto = GetObject("SAPGUI")
Set sapapplication = GuiAuto.GetScriptingEngine
For Each Connection In sapapplication.Children
If Not Connection.DisabledByServer Then
For Each session In Connection.Children
If session.Busy = False Then
If session.info.Transaction = "SESSION_MANAGER" Then
session_nr = session.info.sessionnumber - 1
Set session = Connection.Children(Int(session_nr))
hwnd = session.ActiveWindow.Handle
BringWindowToTop hwnd
session.ActiveWindow.SetFocus
session.TestToolMode = 1
Set cmdattachx = session
Exit Function
End If
End If
Next
End If
Next
If session_nr = -1 Then
For Each Connection In sapapplication.Children
If Not Connection.DisabledByServer Then
For Each session In Connection.Children
If session.Busy = False Then
session.createsession
sessionFound = True
Exit For
End If
Next
If sessionFound = True Then
Exit For
End If
End If
Next
End If
End Function
Sub Test()
Set session = cmdattachx()
If session Is Nothing Then
Application.Wait (Now + TimeValue("0:00:03"))
Set session = cmdattachx()
End If
End Sub
Let us know your results.
Cheers
Stefan