cancel
Showing results for 
Search instead for 
Did you mean: 

Have script use current session

Former Member
0 Kudos

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.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

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.

script_man
Active Contributor
0 Kudos

Hi j.macc,

Thank you for the compliment. How run you the scripts? From SAP or by double-clicking in the Windows Explorer or ...

This information is very important for the solution.

Regards,

ScriptMan

Former Member
0 Kudos

Hi Scriptman,

I have been running it from windows, by double clicking on the script. I believe it then uses wscript.exe which is windows script host. I would ideally like to keep using this method since the user can float a windows toolbar over their SAP sessions and simply click on the shortcut to run the script. It is a few less steps than dragging and dropping or using the sap script playback option.

If it can be done through this method it would be great, otherwise I am open to other suggestions.

script_man
Active Contributor
0 Kudos

Hi j.macc,

In this case, my suggestion would be as follows:

1. Place a link on the desktop to the script.

2. Find a suitable hotkey and save it in the properties of the link.

3. The scripts should begin with the following lines:


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
set session = application.ActiveSession
' Here follows the rest of the script.

4. Attach the desired SAP session.

5. Press the agreed hotkey.

Within the script you could also evaluate the title and then decide what to do. When we run a script with a hotkey, the focus stays where it was before.

An intelligent variant could also be GuiXT for example. If you have time and inclination then look at the commands "image" and "pushButton". The main difference between the two would be that of one is free and the other is not free.

http://www.synactive.com/docu_e/index_docu_e.html

Regards,

ScriptMan

Former Member
0 Kudos

Hi Scriptman,

What if i am trying to run this through Excel Macro codes? It doesn't seem to work when i tried. It says it needs an object.

Hoping you can help.

Thanks.

Regards,

Kevin

script_man
Active Contributor
0 Kudos

Hi Kevin,

You would publish here your VBA - program to offer you help.

Regards,

ScriptMan

Answers (3)

Answers (3)

Former Member
0 Kudos

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

Former Member
0 Kudos

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

script_man
Active Contributor
0 Kudos

Hi j.macc,

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

Regards,

ScriptMan