Skip to Content
author's profile photo Former Member
Former Member

Have script use current session

Hello,

I am an SAP end user and do not have a programming background, but am teaching myself VBScript to automate some processes. I have learned quite a bit through reading through the posts on the forums, but I can not figure out how to get the scripts that I write to run on the current sap session. They always run on the first session that is opened.

It seems like there must be something that can be written in the script to use the current session vs the 1st session that is opened. I have found a few threads on the forums that make mention of this problem, but have not found a solution that works.

Any help would be greatly appreciated.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

4 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Sep 13, 2011 at 01:23 PM

    Hi Scriptman,

    I have learned a lot reading from your previous posts on various subjects. Here is the start of the script just up to the point where it gets the title of the transaction. It always runs on first session created unless I change the children number to reflect the session that I want it to run on.

    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

    trans = session.findById("wnd[0]/titl").text

    I have tried a couple scripts that you have previously posted and the one that comes closest to doing what I want is :

    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

    msgbox "Please select a session within the next 3 seconds after clicking ok"

    wscript.sleep 3000

    set session = application.ActiveSession

    trans = session.findById("wnd[0]/titl").text

    I would like it to just run on the last session that the user was last accessing without them having to click on the session to activate it after the script is running. Do you know of any way to do this or if it is possible?

    Thanks again for all the great information that you have already posted.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Sep 10, 2011 at 05:26 PM

    Hi j.macc,

    Perhaps there is a solution. Before, I would need to know how to start the scripts.

    Regards,

    ScriptMan

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 14, 2011 at 01:19 PM

    Thank you Scriptman.

    I knew there was a simple solution. The creating a shortcut to the script and assigning it a hotkey did just what I need.

    Thank you for your other suggestion as well. I will look into that.

    J macc

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Feb 08, 2016 at 10:47 PM

    Hello Scriptman/James,

    this code segment worked for me as well

    msgbox "Please select a session within the next 3 seconds after clicking ok"

    wscript.sleep 3000

    set session = application.ActiveSession

    Thanks so much for this

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.