cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Activate a session via Gui scripting

Former Member
0 Likes
10,110

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

View Entire Topic
Former Member
0 Likes

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 Likes

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 Likes

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 Likes

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 Likes

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 Likes

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 Likes

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 Likes

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