0

I have a simple script that hunts for popup boxes that are generated for website and Excel. It works most of the time but errors out occasionlly and seemingly randomly. The error is line: 6 Char: 3 Error: Invalid window handle Code: 80070578

I can't figuer out why it'll work for hours then error seemingly at random. Also the script uses a lot of CPU if anyone could advise how to make it more efficent. Thanks For your time

Set WshShell = CreateObject("WScript.Shell")
Do
    Do
    ret = WshShell.AppActivate("Message from webpage") 
    ret2 = WshShell.AppActivate("Microsoft Excel")
    Loop Until ret = True or ret2 = True

WScript.sleep 500
ret = WshShell.AppActivate("Message from webpage")
ret2 = WshShell.AppActivate("Microsoft Excel")
    If ret = True or ret2 = True Then
        WScript.Sleep 200
        WshShell.SendKeys("{ENTER}")
    End If
WScript.sleep 500
Loop
Emein
  • 37
  • 5
  • Simple loop? If you say so. – user692942 Oct 30 '21 at 18:15
  • Is it overly complicated? I thought it was pretty strait forward. – Emein Oct 30 '21 at 18:18
  • `Do` loops done incorrectly can lead to memory leaks, CPU spikes and even make your system unresponsive. This has two of them, not only that there nested as well. – user692942 Oct 30 '21 at 18:20
  • Can you point me in the direction of a better method or how to fix what i have? I need it to to continuely run which is why i have the first Do. – Emein Oct 30 '21 at 18:32
  • VBScript is not designed to continually run, use the correct tool for the correct job. What is it you are trying to do? – user692942 Oct 30 '21 at 20:22
  • wait for popup boxes that are generated from an internet explorer website and press enter or close them – Emein Oct 30 '21 at 21:19
  • The simple answer is VBScript is not designed for continual scripts. Hacks like using `WScript.Sleep()` to add a delay or allow the OS to handle any events without tying up the processing thread is simply that a hack. The longer a script like this runs the more likely a memory leak will occur and CPU usage will gradually increase. – user692942 Nov 01 '21 at 11:01

1 Answers1

0

The simple answer is VBScript is not designed to run scripts continuously.

As for what you can do the improve it, let's dissect it a little.

The outer loop is running every 500 milliseconds, which means that every half a second the script is triggered forever this is heavy for what it is doing and will likely cause heavy CPU usage the longer it runs.

Does the outer loop need to run so often, could you not increase the Sleep() from 500 to say 1000 or even 5000 to reduce the load on the CPU?

Better yet you could drop the outer loop completely and run the task using Windows own Task Scheduler which will allow you to create a scheduled task that will execute the script however often you wish. That way the script runs to completion closes releases the memory and is then run again at the next interval.

user692942
  • 16,398
  • 7
  • 76
  • 175
  • The outter loop needs to run every couple of minutes-ish. So I have automaion that opens a web paged this "messages from web" box pops up and I need to cloes it for the automation to go do it's work. When it's done with that job it goes back to main page and to the next job. Depending on job size it could be 30 seconds to 3 min so schedueling isn't really going to work. – Emein Nov 05 '21 at 14:42
  • @Emein in which case, don't use VBScript. Instead, use a programming language that is designed to handle being continuously run or expect the performance issues to continue. You still have the option of increasing the delays instead of running every couple of hundred milliseconds, but it won't fix the underlying problem. – user692942 Nov 05 '21 at 14:49