cancel
Showing results for 
Search instead for 
Did you mean: 

VBS Script: Wait, Pause, Sleep

Former Member
0 Kudos

Dear EXperts,

Newbie here.

I was able to record something (picking progress in TC LT45) and thus generate a VBS file.

Now, running this script is doing what it should do, except for the timing: It runs way too fast and therefore I am searching for adding some command/code to pause.

I found examples like Application.Wait("0:00:1000") but SAP does not like that.

See 'my' code below.

Any answer will be appreciated.

Thanks, Nick

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]").resizeWorkingPane 263,47,false

session.findById("wnd[0]").sendVKey 8

Application.Wait("0:00:1000")

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

Application.Wait("0:00:5000")

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

Application.Wait("0:00:1000")

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

Edited by: N. Lagace on Mar 8, 2012 1:37 PM

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hello N. Lagace,

in vbs-scripts you have to use this::

wscript.sleep milliseconds

e.g.

. . .

wscript.sleep 6000

. . .

The script will wait 60 seconds an then continue the code.

I allways use it.

Look at:

http://technet.microsoft.com/en-us/library/ee156589.asp

Listing 3.13 Setting the Time-out Value of a Script

Greetings,

netkid

script_man
Active Contributor
0 Kudos

Hi netkid,

absolutely right, just a little note: 6000 miliseconds = 6 seconds

It does not work if the script is started from within SAP. Then you should use a different method.

Regards,

ScriptMan

Former Member
0 Kudos

Hey Scriptman,

I knew you would respond to my post.

The WScript.Sleep 4000 I already had in place, and yes I start the thing from SAP, and you are right it does not work becaus it says Object Required: WScript

how to deal with that object requirement?

So, your advise to use an other method, is that feasible for a newbie? I already was proud to be digging in VB as a strictly functional guy.

Thanks,

Nick

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]").resizeWorkingPane 263,47,false

session.findById("wnd[0]").sendVKey 8

WScript.Sleep 4000

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

WScript.Sleep 4000

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

WScript.Sleep 4000

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

Edited by: N. Lagace on Mar 9, 2012 12:17 PM

Former Member
0 Kudos

Hey Netkid,

Thank you for responding to my post.

The WScript.Sleep 4000 I already had in place, and yes I start the thing from SAP, and you are right it does not work becaus it says Object Required: WScript

how to deal with that object requirement?

Thanks,

Nick

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]").resizeWorkingPane 263,47,false

session.findById("wnd[0]").sendVKey 8

WScript.Sleep 4000

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

WScript.Sleep 4000

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

WScript.Sleep 4000

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

session.findById("wnd[0]").sendVKey 3

Former Member
0 Kudos

Hello ScriptMan,

thank you for yor note. The same mistake is at the microsoft page . . .

The statement "wscript.sleep xxxx" does work on my computer.

This little vbs-program works when I start it in SAP itself

and i don't kwow why.

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

wscript.sleep 3000
session.findById("wnd[0]/tbar[0]/okcd").text = "SP01"		'Spools
wscript.sleep 3000
session.findById("wnd[0]/tbar[0]/btn[0]").press
wscript.sleep 3000
session.findById("wnd[0]/tbar[1]/btn[8]").press
wscript.sleep 3000
session.findById("wnd[0]/usr/chk[1,3]").selected = true
wscript.sleep 3000
session.findById("wnd[0]/usr/chk[1,3]").setFocus

Set SapGuiAuto = Nothing
Set application = Nothing
Set connection = Nothing
Set session = Nothing

I work with SAP-Frontend für Windows (720 Final Release) and

Microsoft Windows XP 5.1 (2600) Service Pack 3.

Maybe the reason is: there is C:WINDOWSsystem32VBScript.dll (Version 5.7.6002.22589) loaded.

I don't know.

Regards,

netkid

Edited by: netkid on Mar 9, 2012 1:04 PM

Former Member
0 Kudos

Hey Netkid & Sriptman,

In that case: My VBScript.dll version in System32 is 5.8.7600.16762

Thanks,

Nick

Edited by: N. Lagace on Mar 9, 2012 1:42 PM

Former Member
0 Kudos

Scriptman,

Actually, I just noticed that my code below does perform the first step, (simulate F8), and only then begins to ask about the Object Required: WScript.

That gives me the idea that at least partially the script is running successfully, but needs something to do the second step (simulate F3)

Thanks, Nick

*********************************************************

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]").resizeWorkingPane 263,47,false

session.findById("wnd[0]").sendVKey 8

WScript.Sleep 4000

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

WScript.Sleep 4000

session.findById("wnd[0]").sendVKey 3

session.findById("wnd[0]").sendVKey 8

WScript.Sleep 4000

script_man
Active Contributor
0 Kudos

Hi Nick,

If you still should not have any other solution to your problem, you can try the following suggestion:

set wshell = createObject("WScript.Shell")

If Not IsObject(application) Then

   Set SapGuiAuto  = GetObject("SAPGUI")

   Set application = SapGuiAuto.GetScriptingEngine

End If

. . .

session.findById("wnd[0]").sendVKey 8

wshell.run "c:\tmp\sleep_4000.vbs",1,true

session.findById("wnd[0]").sendVKey 3

. . .

The file c:\tmp\sleep_4000.vbs contains the following command:

wscript.sleep 4000

Regards,

ScriptMan

Former Member
0 Kudos

Hi Scriptman,

. . . . . it works! I am speechless.... Thank you for your multiple replies; I will reward you as soon as I have found out how that works here.

Again, thanks a lot!

Best regards,

Nick

Former Member
0 Kudos

hi scriptman

how to create this step by step pls....

The file c:\tmp\sleep_4000.vbs contains the following command:

wscript.sleep 4000

coz I cannot open vbs here unless I press alt f11...

Please advise.

Former Member
0 Kudos

Hi Nick,

I deal with that object requirement as follows.

Simply change your first 15 lines in your vbs-code

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

with these 9 new  lines of code

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

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

and then write this line down when it's needed in your vbs code:

wscript.sleep 3000     'waiting for 3000 milliseconds

In every of mine vbs code it still works.

Best regards, netkid

0 Kudos

Hi Scriptman,

We are working on one of the VB Script , i would like to execute in SAP Application server using SM69 configuration ,  but i could not able to do that it is going into error.

Can someone help me on this?

Thank you.

Regards,

Srini

script_man
Active Contributor
0 Kudos

Hi Srini,

It is best to open a new thread and post there your sample script.

Regards,

ScriptMan

Answers (1)

Answers (1)

Former Member
0 Kudos

From a programming standpoint using sleep states and wait states is not optimal, it makes the script unreliable and take longer to run than it strictly needs to. 

I will usually reference fields directly by ID and then write directly to them.  IE:

session.findById("wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL_255-SLOW_I[1,0]").text = "CC*"

or a simpler reference:

session.findById("wnd[0]/usr/txtMAX_SEL").text = "1000000 " 

wnd is the window, then there is a simple hierarchy, so wnd(window number]/[part of window]/[field name]).[text] = [in quotes, the text that should go into the field, or a variable/parameter (no quotes for those)]

You can use the script recording feature to find out what the individual fields references are, or you can use GuiXT, navigate to a field, and note the field name in the GuiXT info box.

Using direct references like this, the system will automatically wait until a field entry is completed before moving to the next one. 

I feel this is a more elegant solution than programing wait periods into the script.