Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script hangs or doesn't always work correctly

Status
Not open for further replies.

foxup

Programmer
Dec 14, 2010
328
CA
Hi,

I'm at a a loss. I have the below script which will work sometimes and sometimes it doesn't work. From what I've witnessed, it seems to not send the {ENTER} key (in the middle after typing the path and filename). It is launched by a task scheduler.

Here is the script:

Set WshShell = WScript.CreateObject("WScript.Shell")
WSHShell.Run ("firefox") ' run the app
Wscript.Sleep(2000)
wshshell.sendkeys "j"
Wscript.Sleep(250)
wshshell.sendkeys "{DOWN}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "grappa"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
wshshell.sendkeys "{ENTER}"
Wscript.Sleep(3500)
wshshell.sendkeys "%F"
Wscript.Sleep(100)
wshshell.sendkeys "{ENTER}"
Wscript.Sleep(2000)
wshshell.sendkeys "Wscript.Sleep(250)
wshshell.sendkeys "{ENTER}"
Wscript.Sleep(3500)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{ENTER}"
Wscript.Sleep(1000)
wshshell.sendkeys "C"
Wscript.Sleep(50)
wshshell.sendkeys ":"
Wscript.Sleep(50)
wshshell.sendkeys "\"
Wscript.Sleep(50)
wshshell.sendkeys "c"
Wscript.Sleep(50)
wshshell.sendkeys "r"
Wscript.Sleep(50)
wshshell.sendkeys "m"
Wscript.Sleep(50)
wshshell.sendkeys "_"
Wscript.Sleep(50)
wshshell.sendkeys "i"
Wscript.Sleep(50)
wshshell.sendkeys "m"
Wscript.Sleep(50)
wshshell.sendkeys "p"
Wscript.Sleep(50)
wshshell.sendkeys "_"
Wscript.Sleep(50)
wshshell.sendkeys "e"
Wscript.Sleep(50)
wshshell.sendkeys "x"
Wscript.Sleep(50)
wshshell.sendkeys "p"
Wscript.Sleep(50)
wshshell.sendkeys "\"
Wscript.Sleep(50)
wshshell.sendkeys "v"
Wscript.Sleep(50)
wshshell.sendkeys "t"
Wscript.Sleep(50)
wshshell.sendkeys "i"
Wscript.Sleep(50)
wshshell.sendkeys "g"
Wscript.Sleep(50)
wshshell.sendkeys "e"
Wscript.Sleep(50)
wshshell.sendkeys "r"
Wscript.Sleep(50)
wshshell.sendkeys "d"
Wscript.Sleep(50)
wshshell.sendkeys "a"
Wscript.Sleep(50)
wshshell.sendkeys "t"
Wscript.Sleep(50)
wshshell.sendkeys "a"
wshshell.sendkeys "."
Wscript.Sleep(50)
wshshell.sendkeys "c"
Wscript.Sleep(50)
wshshell.sendkeys "s"
Wscript.Sleep(50)
wshshell.sendkeys "v"
Wscript.Sleep(2000)
wshshell.sendkeys "{ENTER}" <<<-- This is where I've seen it not send the Enter key
Wscript.Sleep(2000)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{ENTER}"
Wscript.Sleep(5000)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys " "
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{DOWN}"
Wscript.Sleep(1500)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys " "
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{DOWN}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{ENTER}"
Wscript.Sleep(120000)
wshshell.sendkeys "%{F4}" <<-- This is where I've seen it not close the window


It also doesn't "ALT-F4" sometimes.

Any help p-l-e-a-s-e. As I mentionned, sometimes it works perfectly, other times, it doesn't 'send' thoses keystrokes.

Please help.


Thanks,
FOXUP
 
It is recommended to use the AppActivate method just before each call to SendKeys.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi,

It seems to help a little but i think I know where your going with this.

This code:

wshshell.sendkeys "{ENTER}" <<-- This actually opens a "File Upload" dialog box and it seems that it's here where the problem starts.

Any help please.

The keys that are sent in this dialog box are not always sent properly. Sometimes they are & sometimes they're not.

Please help,
Thanks,
FOXUP!
 
Is there anyway of setting focus to the 'front window'. Plese help.


Thanks,
FOXUP
 
Again, have a look at the AppActivate method.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I did.

The title bar of the "File Upload" dialog is "File Upload", so I put:

WshShell.AppActivate ("File Upload")

but it's still not giving it 'focus'.

As I asked previously, is there any way of automatically putting focus on the current open window. Like I said, I tried WshShell.AppActivate ("File Upload") bit it doesn't do it.


Any help please,
Thanks,
FOXUP!
 
I experienced a similar flakiness with AppActivate with Security Warning and File Download windows. My resolution was to put it in a timed loop. The loop runs until a) focus is brought to the window or b) the timeout is reached.

Code:
strStart = now
do until (objShell.AppActivate("Security Alert") OR (datediff("s", strStart, now) > 5) 
loop

-Geates

Note: You can issue a string of keys with .sendkeys instead of doing one at a time.

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Hmmmm, I like your note. Is there any way of making this shorterr:

wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
wshshell.sendkeys "{TAB}"
Wscript.Sleep(250)
 
Shorter:
Code:
Dim x
For x = 1 to 31
   wshshell.sendkeys "{TAB}"
   Wscript.Sleep(250)
Next
 
Geates,

It still seems to be happening. I've put an app activate before each sendkeys and it still seems to be happening. It,s not happening as much mind you but still an issue nonetheless. as mentioned, it happens when the "File Upload" popup comes up.

I've also optimized my code thanks to guitarzan.

Can anybody help please. I really need to have this ,flakiness, fixed please.

Thank, FOXUP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top