$(function () { pageContext.i18n.modTalk = 'moderation talk'; pageContext.i18n.replyToComment = 'Reply'; pageContext.i18n.modTalkEmpty = 'moderation talk is empty'; pageContext.url.getModTalk = "/comments/%25ID%25/listModTalk.json"; pageContext.url.possibleCommentRecipients = "/comments/%ID%/possibleRecipients.json"; pageContext.url.commentEdit = '/comments/%25ID%25/edit.html'; pageContext.url.commentView = '/comments/%ID%/view.html'; pageContext.i18n.commentVisibility = { 'full': 'Viewable by all users', 'op': 'Viewable by the original poster', 'mod': 'Viewable by moderators', 'opAndMod': 'Viewable by moderators and the original poster', 'other': 'Advanced visibility', 'dialogTitle': 'Comment visibility', 'selectGroups': 'Visible to groups', 'selectOther': 'Other recipients', 'selectOriginalPoster': 'Original poster', 'selectModerators': 'Moderators', 'selectAssignees': 'Asked to answer users' }; pageContext.i18n.commentMenuLabels = { 'comment-edit': 'comments.menu.edit', 'comment-delete': 'comments.menu.delete', 'comment-convert': 'comments.menu.convert' };pageContext.i18n.answer= { bestAnswer: 'Best Answer', controlBar : { accept: 'Accept', unaccept: 'Unaccept', acceptCommand: 'Accept this answer as correct', cancelAcceptedCommand: 'Remove this answers accepted status' } }; window.croles = { u: false, op: false, m: false, og: false, as: false, ag: false, dc: false, doc: false, eo: false, ea: false }; tools.init({ q: { e: false, ew: false, eo: false, r: false, ro: false, d: false, dow: false, fv: false, c: false, co: false, p: false, tm: false , ms: false, mos: false }, n: { f: false, vf: false, vfo: false, vr: true, vro: true, c: false, co: false, vu: false, vd: false, w: false, wo: false, l: false }, c: { e: false, eo: false, d: false, dow: false, ta: false, tao: false, l: false }, a: { e: false, ew: false, eo: false, d: false, dow: false, a: false, aoq: false, ao: false, tc: false, tco: false, p: false, tm: false }, pc: croles }, { tc: true, nsc: true }); commandUtils.initializeLabels(); }); Skip to Content
avatar image
Former Member

Activate a session via Gui scripting

Hi,

how to activate an active SAP-session via Gui Scripting or maybe a workaround?

There are some SAP sessions running and I want to activate a certain one of them.

I tried to activate a session with:

AppActivate session.findById("wnd[0]").Text

but, as we know, if there are the same task names in Task Manager, the function will start any one of them.

Any idea?

Edited by: Stefan Benkö on Feb 9, 2009 7:07 AM

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • avatar image
    Former Member
    Mar 10, 2009 at 04:29 AM

    You could count the SAP connectionsans sessions, then select one to activate using a form.

    I have had sucess populating an array with the connections and sessions, then using that array to build a form for the user to select the connection and session. Then use the user's selection to set the connection and session objects.

    Or you could display all connections and sessions and a dialog box, and have the user specify the connection and session.

    You will need to subtract 1 from the result to convert to base 0. For example, if this shows that you have 1 connection, then subtract 1, and set your connection to 0. Example: "Set connection = application.Children(0)"

    Here is an example in VBS, similar to a recorded script. I use Excel VBA to build the form rather than VBS.

    If Not IsObject(application) Then

    Set SapGuiAuto = GetObject("SAPGUI")

    Set application = SapGuiAuto.GetScriptingEngine

    End If

    '

    iConnections = application.Connections.Count

    msgbox iConnections & " Connections"

    '

    iSessions = application.Connections.Item(0).Sessions.Count

    msgbox isessions & " Sessions"

    '

    sSAP_Session = Application.Connections.Item(0).Sessions.Item(0).Info.Transaction

    '

    msgbox sSAP_Session & " 1st session"

    '

    If Not IsObject(connection) Then

    Set connection = application.Children(0)

    End If

    If Not IsObject(session) Then

    Set session = connection.Children(0)

    End If

    If IsObject(WScript) Then

    WScript.ConnectObject session, "on"

    WScript.ConnectObject application, "on"

    End If

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 16, 2009 at 02:21 PM

    Dear Casmir,

    I use the following code:

    suFound = False

    suError = False

    sgSystem = Trim(Cells(Target.Row, scSysCol))

    sgClient = Trim(Cells(Target.Row, scCliCol))

    sgUser = Trim(Cells(Target.Row, scUsrCol))

    sgPW = Trim(Cells(Target.Row, scPwdCol))

    suError = True

    suError = Not ssApplication

    If suError Then GoTo StopSub

    For Each suConnection In sgApplication.Connections

    suSysName = suConnection.Description

    If suSysName = sgSystem Then

    For Each suSession In suConnection.sessions

    suSysClient = suSession.info.client

    If suSysClient = sgClient Then

    AppActivate suSession.findById("wnd[0]").Text

    'Aktive Instanz herausfinden, SID/Client checken

    Set suAkt = sgApplication.ActiveSession()

    suAktName = suAkt.Parent.Description

    suAktClient = suAkt.info.client

    If sgSystem <> suAktName Or sgClient <> suAktClient Then

    If Not suFound Then

    suFound = True

    AppActivate "Microsoft Excel"

    A = MsgBox(sgSystem & " - " & sgClient & " NOT selectable", vbOKOnly + vbCritical + vbDefaultButton1, "SAP Automation")

    End If

    Else

    suFound = True

    End If

    Exit For

    End If

    suSysClient = ""

    Next

    End If

    If suFound Then Exit For

    suSysName = ""

    Next

    As you can see, I got at that point all the necessary session and connection data.

    There are several SAP-Tasks open and what I want is to get a specific one as active window on my screen. That doesn't work if the sessions have the same Task-name, for instance "SAP Easy Access". Because Windos is not albe to find the right SAP-task - they have the same name - and it takes usally the wrong one of them. Thats my problem.

    Many Thanks for your Help

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Mar 17, 2009 at 12:45 PM

    Hi Stefan,

    I have had success identifying the Excel VBA objects by viewing the Locals Window in Excel VBA while stepping through the code.

    The sessions will be in a collection beginning with item 0. You can select a session and specify the session using the collection item number.

    If you need to activate the specified session, try GetObject ("SAPGUI")

    Best Regards,

    Casimir

    Add comment
    10|10000 characters needed characters exceeded

    • Hi,

      a general solution, I have not yet. Perhaps some of you have it perfect.

      If Not IsObject(application) Then

      Set SapGuiAuto = GetObject("SAPGUI")

      Set application = SapGuiAuto.GetScriptingEngine

      End If

      If Not IsObject(connection) Then

      Set connection = application.Children(0)

      End If

      If Not IsObject(session) Then

      Set session = connection.Children(0)

      End If

      If IsObject(WScript) Then

      WScript.ConnectObject session, "on"

      WScript.ConnectObject application, "on"

      End If

      set aw = session.activeWindow()

      aw.findById("wnd[0]").maximize

      on error resume next

      SESSION_ = 0

      do

      err.clear

      aw.findById("wnd.......").press

      if err.number = 0 or SESSION_ = (connection.children.count - 1) then exit do

      SESSION_ = SESSION_ + 1

      Set session = connection.Children(int(SESSION_))

      set aw = session.activeWindow()

      loop

      set aw1 = session.activeWindow()

      aw1.findById("wnd[1]/tbar[0]/btn[0]").press

      on error goto 0

      There is still a problem when multiple modes include the same transaction.

      Regards,

      ScriptMan

      Edited by: ScriptMan on Jun 25, 2009 8:20 AM

  • Jul 06, 2009 at 05:22 PM

    Hello Stefan,

    If I understand correctly, you are logged into different instances e.g. ECD(100), ECD(120), ECQ(100) etc. and you want to pick a specific log-in.

    Here is what I do in script. (Where W_System = ECQ100 etc.)

    Dim il, it

    Dim W_conn, W_Sess, W_System

    W_System = objSheet.Cells(iRow, 1)

    If Not objSess Is Nothing Then

    If objSess.Info.SystemName & objSess.Info.Client = W_System Then

    Exit Function

    End If

    End If

    If objGui Is Nothing Then

    Set SapGuiAuto = GetObject("SAPGUI")

    Set objGui = SapGuiAuto.GetScriptingEngine

    End If

    For il = 0 To objGui.Children.Count - 1

    Set W_conn = objGui.Children(il + 0)

    For it = 0 To W_conn.Children.Count - 1

    Set W_Sess = W_conn.Children(it + 0)

    If W_Sess.Info.SystemName & W_Sess.Info.Client = W_System Then

    Set objConn = objGui.Children(il + 0)

    Set objSess = objConn.Children(it + 0)

    Exit For

    End If

    Next

    Next

    If objSess Is Nothing Then

    MsgBox "Not Logged into system"

    End If

    If IsObject(WScript) Then

    WScript.ConnectObject objSess, "on"

    WScript.ConnectObject objGui, "on"

    End If

    Hope this helps. Further if you have multiple sessions in the system you can add further check e.g. "Transaction" property etc.

    Best regards,

    Nitin

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Aug 06, 2009 at 03:19 PM

    I am also looking for a solution to this issue, which seems to be what this thread is about:

    - I would like the users to activate a script on a standard SAP screen

    - THe script will grab a field from the current screen

    - in the command line it types '/o<a report transaction>" and creates a new session with that transaction running.

    - now I have 2 sessions, one with the original transaction and the 2nd with the new report transaction

    - I want to switch to the report transaction and paste the value I read from the 1st session into a field and execute the report

    ScriptMan - can I use the last code you have in this thread?

    Thanks

    Umur

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Umur,

      how about ...

      1. create a link to SAP via Alt / F12 on your desktop e.g. like "S11 Login.sap"

      2. create a link to the autoscript.vbs in the script directory that will be started automatically e.g. like "autoscript.vbs.lnk"

      3. create Start_Script.vbs

      For example:

      set Wshell = CreateObject("WScript.Shell")

      Do

      bWindowFound = Wshell.AppActivate("SAP Easy Access")

      WScript.Sleep 1000

      Loop Until bWindowFound

      wshell.run chr(34) & "C:\. . .\autoscript.vbs.lnk" & chr(34), , false

      3. create Start_Sap.bat on your desktop

      For example:

      "S11 Login.sap"

      "Path + start_script.vbs"

      4. Done

      Regards,

      ScriptMan