cancel
Showing results for 
Search instead for 
Did you mean: 

Script and Multiple Sessions.

Former Member
0 Kudos

Hi everyone. This is my first post here, and I'll admit I'm rather inexperienced with scripting, so any help you can provide is appreciated!

We are using a new document imaging program that retrieves values from SAP using scripting. There seems to be a issue when people open multiple SAP sessions, it keeps wanting to retrieve the values from the first session opened.

We would like to have it so what ever SAP session that was last used before running the script, is the values that are used.

Below is a example of one of our scripts. The field3, tab and folder values are values in my imaging system. This script works, but again it will only take the values of the first SAP session opened. We want one that can take it any active session, not just the first.

Set SapGuiAuto = GetObject("SAPGUI")

Set application = SapGuiAuto.GetScriptingEngine

Set connection = application.Children(0)

Set session = connection.Children(0)

field3=session.findById("wnd[0]/usr/ctxtBKPF-BUDAT").text

tab=session.findById("wnd[0]/usr/txtBKPF-XBLNR").text

folder=session.findById("wnd[0]/usr/ctxtRF05A-NEWKO").text

Thanks in advance for any suggestions!

Accepted Solutions (0)

Answers (1)

Answers (1)

RMW
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Jason,

see the "Programming API", which is available under "SAP GUI scripting" at http://service.sap.com/sapgui .

The GuiApplication object has a property ActiveSession, which points to the Session that the user is currently working with, which will be the topmost window.

Best regards

Rolf-Martin

Former Member
0 Kudos

Hi Rolf,

Unfortunately I don't have a user name and password for the SAP site... Our parent company has all of that information, I asked them today for the ID but have not heard back.

I did a google search and was able to find some info on a web site reading

"session.activeWindow()" but it didn't seem to help.

From what I found from that site I tried the below, but it still didn't seem to work.

Set aw = session.activeWindow()

field3= aw.findById("wnd[0]/usr/ctxtBKPF-BUDAT").text

RMW
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello Jason,

when you get the window from the session, then your findById() should not ask for the window again.

So what you should try is something like

Set aw = session.activeWindow()
field3= aw.findById("usr/ctxtBKPF-BUDAT").text 

Best regards

Rolf-Martin

Former Member
0 Kudos

Rolf,

I've adjusted my code (see below full code), but it still seems to want to take data only from the first session. If the script is run outside of SAP, would the activewindow thing still detect the last active SAP window?

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()

field3 = aw.findById("usr/ctxtBKPF-BUDAT").text

RMW
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi again,

the problem is caused by

Set connection = application.Children(0)

and

Set session = connection.Children(0)

That way you will always get the first session of the first connection, but not the front most window of all open sessions.

Could you try to make use of ActiveSession of the application object instead, like suggested before?

This also has been discussed in .

Best regards

Rolf-Martin

Former Member
0 Kudos

Rolf,

Again I'm very inexperienced with scripting... I adjusted the code to what is shown below... And I now receive the following error... "Script error detected at line 3. Description: Object doesn't support this property or method: 'application.ActiveSession"

Set SapGuiAuto = GetObject("SAPGUI")

Set application = SapGuiAuto.GetScriptingEngine

Set session = application.ActiveSession

field3 = session.findById("usr/ctxtBKPF-BUDAT").text

RMW
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hello,

for making application.ActiveSession return an object, a session needs to be in foreground.

E.g. the "attaching script" message window should be turned off.

Then

Set session = application.ActiveSession
field3 = session.findById("wnd[0]/usr/ctxtBKPF-BUDAT").text

should work.

Best regards

Rolf-Martin

Former Member
0 Kudos

Hello,

I am actually trying to accomplish the same thing.

I am using this code.

Set SapGuiAuto = GetObject("SAPGUI")

Set application = SapGuiAuto.GetScriptingEngine

Set session = application.ActiveSession

field3 = session.findById("/wnd[0]/usr/ctxtBKPF-BUDAT").text

and receive the error: Object Required - session.

Thoughts?

Former Member
0 Kudos

Hi Steve,

Unfortunately I have not got this working yet either. So please if you do get it, post the results, as I will do the same.

Regards,

Jason Incarnato

Former Member
0 Kudos

I will definitely do that. I am having some developers I know look at it as well, so hopefully we will have something soon.

Steve