cancel
Showing results for 
Search instead for 
Did you mean: 

Activate a session via Gui scripting

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (5)

Answers (5)

Former Member
0 Kudos

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

script_man
Active Contributor
0 Kudos

Hi Umur,

It's nice to once again to hear from you. After a certain time has automatically each one of us the same questions.

In the code above, I tried to find a solution to this problem:

- It has, for example, all 6 modes open.

- A script is started

- Itt is be executed in the active transaction.

- One knows not what the session is active.

After a switch, or a certain specific value without an error is found, it is assumed that is the requested transaction.

If you know the active session, it should not be so big problem. The solution might look like this.

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

. . .

'script for the original transaktion without the first 14 Rows

. . .

session.createsession '

set Wshell = CreateObject("WScript.Shell")

wshell.run "c:\temp\sleep.vbs",1,true

Set session = connection.Children(1)

. . .

'script for the new session without the first 14 Rows

. . .

Sleep.vbs contains a single line: Wscript.Sleep 1000

After opening a new mode, you have to wait a while until the new window is responsive. Otherwise an error will be reported.

Regards,

ScriptMan

Former Member
0 Kudos

Hi ScriptMan - was away for a while. I tried your code but I get an 'unkown error' message with sleep script execute. I created the fiel and saved it in the right place. When I execute the script, I create the session 2, get there but all I need to do there is somehow actiuvate that session so I can fill the data on that session 2 screen, execute the report, and switch the session back to 1. I thought the command "Set session = connection.Children(1)" was for this but does not want to work.

anyway if this is going to be too complicated, I can forget about it.

Regards

Umur

script_man
Active Contributor
0 Kudos

Hi Umur,

I have an example for you. Here will pass a parameter named as GP on the new mode. A parameter named as TEXT in the new mode is selected and returned to the original mode.


If Not IsObject(application) Then
. . .
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd" ).text = "/nrecn"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/subSUB_OBJSEL:SAPLRECN_GUI_CN_SEL:1000/ctxtRECN_CONTRACT_X-BUKRS").text = "1234"
session.findById("wnd[0]/usr/subSUB_OBJSEL:SAPLRECN_GUI_CN_SEL:1000/ctxtRECN_CONTRACT_X-RECNNR").text = "1002"
session.findById("wnd[0]/tbar[1]/btn[5]").press
GP = session.findById("wnd[0]/...FASTENTRY:0034/ctxtREGCFLDS_FE-PARTNER").text

session.createsession
set Wshell = CreateObject("WScript.Shell")
wshell.run "c:\temp\sleep.vbs",1,true
Set session = connection.Children(1)
session.findById("wnd[0]/tbar[0]/okcd").text = "/nbp"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/mbar/menu[0]/menu[0]").select
session.findById("wnd[1]/usr/ctxtBUS_JOEL_MAIN-OPEN_NUMBER").text = GP
session.findById("wnd[1]/tbar[0]/btn[0]").press


session.findById("wnd[0]/usr/subSCREEN_3000_...PARTNER_ROLE").setFocus
session.findById("wnd[0]/usr/subSCREEN_3000_...PARTNER_ROLE").key = "X01000"
session.findById("wnd[0]/usr/subSCREEN_3000_...TABSTRIP/tabpSCREEN_1100_TAB_06").select
TEXT = session.findById("wnd[0]/usr/...subA02P01:SAPLFSBP_03:0010/txtBP1010-SOL_TXT").text
session.findById("wnd[0]").close
wshell.run "c:\temp\sleep.vbs",1,true
Set session = connection.Children(0)
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/usr/subSUB_OBJSEL:SAPLRECN_GUI_CN_SEL:1000/ctxtRECN_CONTRACT_X-RECNNR").text = TEXT

Please try to start the script sleep.vbs iindividually. There must be no errors occur. You can make also a link to the following script on the desktop:


set wshell = createObject("WScript.shell")
set args = wscript.arguments
if args.count>0 then
   wshell.run "WScript.exe //x " + chr(34) + args(0) + chr(34)
end if

If you want to try then a script, You need to drag and drop the script on this link and you can try it gradually.

Regards,

ScriptMan

Edited by: ScriptMan on Aug 8, 2009 6:51 PM

Former Member
0 Kudos

Hi ScriptMan - I tried the script you sent. Now 'Sleep' does not give an error but it gets to the set session=connection.children(1) and gives me "Object doesn't support this property or method : 'session' - "

This is the script that works to the last statement below:

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

OrdNo = session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1100/subSUB_KOPF:SAPLCOIH:1102/txtCAUFVD-AUFNR").text

session.createsession

set Wshell = CreateObject("WScript.Shell")

wshell.run "c:\temp\sleep.vbs",1,True

Set session = connection.Children(1)

it is at this point that I want to switch to the new session and do a "/nZMM_ZCOAR002" transaction to paste the OrdNo variable into the new screen.

Best regards

Umur

script_man
Active Contributor
0 Kudos

Hi Umur,

the above script works, if it is started e.g. from a Windows Explorer. Although it is to work under SAP, it must look as follows:


If Not IsObject(application) Then
. . .
Set session    = connection.Children(0)
. . .
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "/nrecn"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/subSUB_OBJSEL:SAPLRECN_GUI_CN_SEL:1000/ctxtRECN_CONTRACT_X-BUKRS").text = "1234"
session.findById("wnd[0]/usr/subSUB_OBJSEL:SAPLRECN_GUI_CN_SEL:1000/ctxtRECN_CONTRACT_X-RECNNR").text = "1002"
session.findById("wnd[0]/tbar[1]/btn[5]").press
GP = session.findById("wnd[0]/usr/subSUB_BDT: . . . ctxtREGCFLDS_FE-PARTNER").text
session.createsession
set Wshell = CreateObject("WScript.Shell")
wshell.run "c:\temp\sleep.vbs",1,true
wshell.run "c:\temp\script1.vbs " & GP,1,true

script1.vbs:


If Not IsObject(application) Then
. . .
   Set session    = connection.Children(1)
. . .
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "/nbp"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/mbar/menu[0]/menu[0]").select
session.findById("wnd[1]/usr/ctxtBUS_JOEL_MAIN-OPEN_NUMBER").text = Wscript.Arguments(0)
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/subSCREEN_3000_RESIZING_AREA: . . .JOEL_MAIN-PARTNER_ROLE").setFocus
session.findById("wnd[0]/usr/subSCREEN_3000_RESIZING_AREA: . . .JOEL_MAIN-PARTNER_ROLE").key = "X01000"
session.findById("wnd[0]/usr/subSCREEN_3000_RESIZING_AREA: . . TABSTRIP/tabpSCREEN_1100_TAB_06").select
TEXT = session.findById("wnd[0]/usr/subSCREEN_3000_RESIZING_AREA: . . .txtBP1010-SOL_TXT").text
session.findById("wnd[0]").close
set Wshell = CreateObject("WScript.Shell")
wshell.run "c:\temp\sleep.vbs",1,true
wshell.run "c:\temp\script2.vbs " & TEXT,1,true

script2.vbs:


If Not IsObject(application) Then
. . .
   Set session    = connection.Children(0)
. . .
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/usr/subSUB_OBJSEL: . . .RECN_CONTRACT_X-RECNNR").text = Wscript.Arguments(0)

Regards,

ScriptMan

Edited by: ScriptMan on Aug 11, 2009 8:57 AM

Former Member
0 Kudos

Hi ScriptMan - works like a charm! Great code, and I already have people lined up to use this functionality. If only there was a way to auto-start a script (pretty much like an autoexec.bat file) when you sign on to SAP...

Thanks again.

Best regards

Umur

script_man
Active Contributor
0 Kudos

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

0 Kudos

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

Former Member
0 Kudos

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

script_man
Active Contributor
0 Kudos

Hi Stefan,

I also have a longer period of time similar problems which I am on a different way to solve that. A professional solution to this problem is haggard to me thanks "GuiXT".

For example:

1. It is a script to:

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

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

...

2. It replaces the first 14 lines as follows:

set aw = session.activeWindow()

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

...

3. If a window change is taking place:

set aw = session.activeWindow()

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

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

set aw1 = session.activeWindow()

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

...

I think that this problem is not only both of us interested. Unfortunately, this script only adapted from SAP to start. At a general validity is still under construction.

Regards,

ScriptMan

Edited by: ScriptMan on Jun 23, 2009 2:24 PM

Edited by: ScriptMan on Jun 23, 2009 2:51 PM

script_man
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

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