Link to home
Start Free TrialLog in
Avatar of bsncp
bsncpFlag for Afghanistan

asked on

SAP login script session count

This code below works nicely.  It logs me into SAP and I have some code beyond this that does some table reading, exporting and so on in SAP.  But this code kills the current login and simply logs right back into SAP.  So if I want to do a nested macro with multiple steps that include this code, it kills SAP and logs in again each time.  Instead, I'd like for the code to first count how many sessions are open for the current login.  If there are fewer than 5, I just want it to open a new session.  If the maximum of 5 has been reached, however, then I am okay with killing the current SAP login completely and logging back in.  There is a commented section in this code that might be close to what I want.   Any thoughts on how to tweak this code to accomplish my goal?
Dim Application As Variant

Set SapGuiAuto = GetObject("SAPGUI")
   Set Application = SapGuiAuto.GetScriptingEngine
   Set Connection = Application.OpenConnection("PRD")
   Set SapSession = Connection.Children(0)

If IsObject(WScript) Then
   WScript.ConnectObject SapSession, "on"
   WScript.ConnectObject Application, "on"
End If

SapSession.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "XXXXXXX"
SapSession.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "YYYYYYY"
SapSession.findById("wnd[0]/usr/pwdRSYST-BCODE").SetFocus
SapSession.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 8
SapSession.findById("wnd[0]").sendVKey 0

'In case you are already logged in...
If SapSession.Children.Count > 1 Then
    SapSession.findById("wnd[1]/usr/radMULTI_LOGON_OPT1").Select
    SapSession.findById("wnd[1]/usr/radMULTI_LOGON_OPT1").SetFocus
    SapSession.findById("wnd[1]/tbar[0]/btn[0]").press
End If

Open in new window

Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Open the connection first in your top function.
Then run the loop calling the nested function using the already opened connection.
Close your top function, thus closing the connection as well.

/gustav
Avatar of bsncp

ASKER

Thank you...but I don't understand what you mean by "Open the connection first in your top function"
The top or calling function is the function that calls the nested "macros" or (sub)functions.

/gustav
Avatar of bsncp

ASKER

Okay, thank you. I see what you mean by that. However, I have tried moving sections of this code around and try to run the steps in an order that would accomplish my goal, but can't seem to get the right combination. If my code has the right elements but are perhaps not in the proper order, is it bad form for me to request that you make the modification to the code and post it here?
Well, I can't, as I haven't SAP installed to test with.
But it would be this block that should somehow be moved upwards:

Set SapGuiAuto = GetObject("SAPGUI")
Set Application = SapGuiAuto.GetScriptingEngine
Set Connection = Application.OpenConnection("PRD")
Set SapSession = Connection.Children(0)

If IsObject(WScript) Then
   WScript.ConnectObject SapSession, "on"
   WScript.ConnectObject Application, "on"
End If

Then pass SapSession and other variables you may need as parameters to the nested functions:

Call MyNestedFunction1(SapSession)
Call MyNestedFunction2(SapSession)
etc.

Set SapSession = Nothing
Set Connection = Nothing
Set Application = Nothing
Set SapGuiAuto = Nothing

/gustav
Avatar of bsncp

ASKER

I will give that a shot and reply. Thank you. My nested function works great after it logs in. I am just trying to solve the issue of killing a session when I could just add a session instead.  Once I figure that out...I'm good.
Great!

/gustav
Avatar of bsncp

ASKER

I'm confused.  The block you suggest moving up is already at the very top of my code...can you elaborate?
> .. it kills SAP and logs in again each time.  

Thus, if you want to run similar code five times, run the top code once only my moving it to a top/parent/outer function which calls your now reduced code five times:

Call MyNestedFunction1(SapSession)
Call MyNestedFunction2(SapSession)
Call MyNestedFunction3(SapSession)
Call MyNestedFunction4(SapSession)
Call MyNestedFunction5(SapSession)

Then is closes SapSession. This way SapSession it opened once only.
/gustav
Avatar of bsncp

ASKER

Sorry.  I should have been more clear.  I have jobs scheduled at different times throughout the day that run this code, mostly requiring just one session.  In other words, I am not running all 5 routines at the same time.

The annoyance with this code is if I happen to be logged into SAP already during my normal course of business at the time one of my scheduled routines runs.  It kills my current session and I lose whatever I was doing.  Whenever the code runs, I want it to look to see if a session is already open.  If so...open a new session.  If not, start SAP and open a session.

Right now it starts SAP and a new session every time it runs.
Oh, I see.
But aren't you already performing such a check:

'In case you are already logged in...
If SapSession.Children.Count > 1 Then

Couldn't you invoke a similar check before running the essential code and - if connected - skip opening a new session?

/gustav
Avatar of bsncp

ASKER

Would love to see how that code looks because I've tried different variations of that with no luck.  the line that says "In case you are already logged in..." doesn't seem to work.  again, every time this code runs it kills any open current sessions.
But that is all controlled by:

   Set SapGuiAuto = GetObject("SAPGUI")
   Set Application = SapGuiAuto.GetScriptingEngine
   Set Connection = Application.OpenConnection("PRD")
   Set SapSession = Connection.Children(0)

I guess the comment line should read:

' In case you got logged in ...

Doesn't SAP provide a forum that covers the SAPGUI and VBscript?

I don't run SAP so I have no option for checking or investigating this further.
However, I can locate the parameter "reuse":

http://help.sap.com/saphelp_nwes70/helpdata/EN/cd/38dff9732f441a8026a6af3f9a0c77/content.htm

which indicates that it should be possible to reuse an existing connection.

/gustav
Avatar of bsncp

ASKER

Thank you, but I am stalling out because I don't know how to insert that reuse parameter into my code.
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial