0

I have a VBScript that will open multiple URLs one by one in the same tab for data migration purposes. The problem I'm having is that if my PC or connection slows down, my VBScript will continue to go. The biggest issue is the Ctrl-L and WshShell.SendKeys:

WshShell.SendKeys "^l"
WScript.Sleep 1000
WshShell.SendKeys "https://www.google.com/"
WshShell.SendKeys "{ENTER}"

commands executing. If I close the browser, it will continue to type these. How can I stop a VBScript mid-execution and is there a way to make it stop if I close the browser?

Squashman
  • 13,649
  • 5
  • 27
  • 36
JMV12
  • 965
  • 1
  • 20
  • 52
  • What does this have to do with sending a username and password with a batch-file? Vbscript is not a batch-file. – Squashman Nov 02 '17 at 15:50
  • Dang it, the title from the last question carried over. I'm really sorry. – JMV12 Nov 02 '17 at 15:51
  • `If I close the browser, it will continue to type these` - Why don't you check browser's existence and state before typing? – Pankaj Jaju Nov 02 '17 at 16:59
  • I'll look for that now. I'm sorry for the stupid questions, this is my first time making a VBScript. Thank you. – JMV12 Nov 02 '17 at 17:36
  • 1
    `SendKeys` is unreliable at best, so it should not be used for anything unless there are no other options at all. Whatever data migration you're doing, you're probably far better off using an [XMLHTTP request](https://stackoverflow.com/a/14835871/1630171) or at least the [Internet Explorer COM object](https://stackoverflow.com/a/27444218/1630171). – Ansgar Wiechers Nov 02 '17 at 23:04

1 Answers1

0

The SendKeys method is a last-ditch effort for attempting to automate old programs that don't have any programmatic access. Per its documentation,

Use the SendKeys method to send keystrokes to applications that have no automation interface.

If you're trying to automate a web browser (as might be done in some sort of automated testing), you should use a library designed for automating a web browser. For example, Internet Explorer has a substantial COM Automation interface (see its documentation, though I don't see as many samples using VBScript on MSDN as I used to, as I suspect they want people to move to .NET and PowerShell for this sort of thing). Using an actual object library for the browser you're automating should allow you to directly control it and handle events, such as your desire to do something when a browser window is closed.

If you're just trying to load the data on a web page, rather than relying on a web browser, you can just load the data using the WinHttpRequest object.

If you do in fact need to use SendKeys to automate something, or otherwise want to find a way to stop your script at certain times, the best way is to have your script check to see if it should continue to run before it continues. For example, after your Sleep call, it should check that whatever it's automating is in the state it's expecting. There's not a lot one can check using just VBScript without loading a COM object that supports a more comprehensive interface, but you can get a little use out of running WScript.AppActivate and checking its return value to see if a window you're looking for is still there. If your script determines that it should terminate, it can call the WScript.Quit method to do so.