Monday, 23 April 2012

VBScript: how to schedule the opening of an ASP page


Following an exchange of ideas with Still_ASP, a loyal reader of the web thought, I thought to write something about scheduling events on a Windows server.
Sometimes we might need to trigger some events independently from the actual opening of a page or a specific action taken by a user. If nobody is browsing your page, how could we trigger an event anyway?

To do so, we need to use a little trick that I will shortly explain.

Schedule an event
In a Windows environment we can schedule tasks using Windows Scheduler. In this article I won't explain how to use the tool, because I assume we all know how to do it. It is enough to say that we can schedule any task we want: backups, running programs, killing of specific tasks, opening of web pages and so on.
As you may foresee, we can use Windows Task Scheduler in order to open a specific ASP page, wait for it to end its tasks and then close it. And we are going to use VBScript.

Prepare the file
In a place we prefer, we need to create a new text file. Right click in an appropriate folder and select New->Text Document. Change the new file extension to ".vbs" and give the file an appropriate name. That is because we need to use VBScript and you will notice that the file icon will change to some sort of blueish paper scroll (is it a magic scroll?).
Now we open the file with a text editor (or whatever you prefer) and we start writing the VBScript code.

The VBScript
The code's quite simple. We are going to open a specific web page in Internet Explorer, wait for it to complete the task and then we close the program.
Insert the following code into the vbs file:
Option Explicit
Dim objIE
Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate ""
objIE.visible = true
While objIE.Busy
Set objIE = Nothing
Let me explain what the code is doing.
It creates an object for Internet Explorer, and thus it opens the program. Then it opens the Google web page. We have to be sure that IE is visible (objIE.visible = true).
The code waits while the IE object is busy and just when IE finishes the task (and the web page has been loaded), the code exits IE and set the object to nothing.
It is very simple, isn't it?
Now that we have the file ready to be used, just double click on it and see what happens. If everything is ok, just create a scheduled task that will open the file and we are done.

I hope that you have found the information useful!
Happy coding.


  1. I run scheduled tasks like this however we use them to generate client viewable statistics during the night. We also generate lists of top viewed website profiles and other 'top' things.
    Basically you set the VBS to write an .asp/.inc file (perhaps late at night, when least traffic) which you then include in your page.
    It's a great way to keep your pages loading fast without bogging down the database with intensive tasks.
    Also, if you're including these documents in your pages it's a good idea to write a quick script that checks if the file is available before trying to include it into the page - just in case any errors pop up when the vbs runs.

    1. Reverson, good idea there. Scheduling task is great, especially when your company can't slow down or even stop, even for just a moment. I do a lot of maintenance routines during the night, because it is the only time when I can...

  2. Hi Marco,

    Thanks for mention me in your posting. Only the VBscript give an error when execute. Keep up the good work.

    1. Yes you're there's a mistake. Now I notice. Let me update it (there's a incorrect variable).

  3. Hi Marco,

    I had still problems to call a *.asp page with a task scheduler. I am hosting on a Windows 2008 server. Someone did write a vbs script for me that works perfect.

    - So create a vbs file for example runurl.vbs with the code I written below.
    - upload it to the webserver
    - call the script with a task scheduler

    Call Send_HTTP_Request()
    Sub Send_HTTP_Request()

    'Force the script to finish on an error.
    On Error Resume Next

    'Declare variables
    Dim objRequest
    Dim URL
    Set objRequest = CreateObject("Microsoft.XMLHTTP")

    'Put together the URL link appending the Variables.
    URL = "http://somepage.asp"

    'Open the HTTP request and pass the URL to the objRequest object "GET", URL , false

    'Send the HTML Request

    'Set the object to nothing
    Set objRequest = Nothing

    End Sub

    1. Thanks for sharing it.
      The code I posted is working on Windows 2003 server, Win Xp, Vista and 7. That's where I tested it. The only difference is that I have those machines here next to me (at least for a little while more) and they are not web servers...
      Anyway thanks again and take care!


Comments are moderated. I apologize if I don't publish comments immediately.

However, I do answer to all the comments.