12. VbScript | Internet Explorer: Sendkeys Automation

Remember to try the code on your own so that you retain what you learn. Ask if something doesn’t make sense!

So this tutorial will be about automating VbScript using Internet Explorer. Internet Explorer is special compared to other browsers because Internet Explorer is built into Windows, and can be called by the CreateObject function. This may not be the same for other browsers, since they aren’t a part of windows default programs.

Table of Contents

Calling Internet Explorer

So the first step would be to call the Internet Explorer application using CreateObject. You can the store this action as the ie variable. You can then tell the Internet Explorer Application to Navigate to google. And in order to make the application visible on your display you set the the visible function to 1 or true. Setting the visible function to 0 or false makes the application not appear on your display; essentially making it run in the “background”.

Option Explicit
Dim ie
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "https://www.google.com/"
ie.Visible=1

Output:

The Internet Explorer opens to Google.com

1

Miscellaneous Points

if you want to get ride of the cluttering of the Internet Explorer window, you can set the toolbar to 0 and the status bar to 0 to remove them from the window. Like so:

Option Explicit
Dim ie
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "https://www.google.com/"
ie.Visible=1
ie.Toolbar=0
ie.StatusBar=0

Output:

The Toolbar and Status bars are no longer showing in the Internet Explorer window.

00
Internet Explorer window without the tool or status bar

11

Other Options:

Option Explicit
Dim ie
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "https://www.google.com/"
ie.Visible=1
ie.Toolbar=0
ie.StatusBar=0
ie.Height=560
ie.Width=1000
ie.Top=0
ie.Left=0
ie.Resizable=0

The height & width controls the size of the Internet Explorer window by the number of pixels. So height=560 means the height of the window is 560 pixels. The Top & Left controls where the window is placed on the desktop. So if Top & Left are both set to 0, then the window’s upper left hand corner perfectly fits the Top-Left part of your display. Resizable controls whether the window can be resized to a different height & width manually by the user’s mouse. Resizable at 0 means that user cannot change the dimensions of the window.

Time for Automation!

Google

So users usually use google to search for a key term. This key term they type via keyboard. So to mimic the actions of a user’s keyboard input, we are going to use sendkeys.

Option Explicit
Dim ie,x
Set ie = CreateObject("InternetExplorer.Application")
Set x = CreateObject("wscript.shell")
ie.Navigate "https://www.google.com/"
ie.Visible=1
ie.Toolbar=0
ie.StatusBar=0
ie.Height=560
ie.Width=1000
ie.Top=0
ie.Left=0
ie.Resizable=0
x.sendkeys "cow"
x.sendkeys "{enter}"

Output:

The script opens an Internet Explorer browser that navigates to google.com, inputs the search term “cow”, and inputs the ENTER key to complete the google search.

Facebook

But what if you are using a website like Facebook? You replace the google url with www.facebook.com and plan out what a user normally hits on a keyboard to log into facebook.

Option Explicit
Dim ie,x
Set ie = CreateObject("InternetExplorer.Application")
Set x = CreateObject("wscript.shell")
ie.Navigate "https://www.facebook.com/"
ie.Visible=1
ie.Toolbar=0
ie.StatusBar=0
ie.Height=560
ie.Width=1000
ie.Top=0
ie.Left=0
ie.Resizable=0

wscript.sleep 5000
x.sendkeys "cow"
x.sendkeys "{tab}"
x.sendkeys "pass"

Note that for some websites like Facebook, it may have a longer loading time, such that the sendkeys don’t have enough time to input the keyboard strokes after the page has loaded, such that the login fields aren’t filled. To tell the vbscript to wait until the the facebook page loads, we use the wscript.sleep command to tell it to wait a # of milliseconds. An alternative to wscript.sleep is to use something called the busy command with a wait loop. The busy command simply checks if the application is busy, which in this case is the Internet Explorer application. You then setup a loop to wait a set amount of time until the Internet Explorer application is no longer busy. The script would look something like this:

.
.
.
Do while ie.Busy
wscript.sleep 200
Loop
x.sendkeys "cow"
x.sendkeys "{tab}"
x.sendkeys "pass"

Random Notes:

  • Information needed to replace wscript.sleep with readyState=4 [link]

One thought to “12. VbScript | Internet Explorer: Sendkeys Automation”

  1. Instead of wscript.sleep use readystate = 4 (Page is fully loaded – 4)

    Do Until ie.readyState = 4
    DoEvents
    Loop

What's Your Opinion?