Wednesday, July 24, 2013

Automating Internet Explorer (Setup)

The initial problem with automating Internet Explorer (IE) is that IE is a mess; and when I say a mess, I mean it is like the drunken celebrity whose career is slowly dying in the tabloid sections of the supermarket. What was initially meant to be the premier browser for the internet has ballooned to this bloated mess of a browser we know and grudgingly use today.

The history of IE is something that many bloggers have touched on, and it is almost universally accepted in the tech community as the last-resort browser, used only when some legacy system requires its use. But for the purpose of automation, it is the best option we have. There are probably multiple different ways to automate other browsers, possibly even using bash scripting or something like that; but I, like most people, do not have any knowledge of these systems. Microsoft has done a lot of work in providing a base from which we can automate IE, so why not attempt to use the framework they have built?

I will eventually add some more technical articles to this post that will help outline the structure and core of VBA. This post, however, will largely skip over the technical aspects of what is going on and simply outline how to perform the task at hand. You will need to read some posts to catch up to speed on how to set up VBA and what objects are to make sense of some of the explanations used herein, but I will attempt to keep it as low-level as possible.

Step 1 – The IE and Browser References

Visual Basic for Applications (VBA) has a reference for some useful objects and functions that will assist us in creating our IE automation. To add these references, go to Tools under the VBA development window (see here for explanation on how to get to this) and select References:

This will bring up a window with a list of References, listed in alphabetical order. You will want to select the following: 1) Microsoft Internet Controls, 2) Microsoft HTML Object Library, and 3) Microsoft Scripting Runtime .

Note that the top four references come standard with all VBA projects.

Explanation of References

You may skip this section if you do not wish for more information on what these references do. It is not essential that you know what they do, but it is useful to have background on them. Each reference is provided with a link that leads to a more technical explanation of each.

Microsoft HTML Object Library gives you access to the objects found in an HTML document. This is crucial since any navigation across a webpage is done through the nodes of the DOM. Don’t worry if you don’t understand what this means, just think of it like this:  the DOM (which stands for Document Object Model) is a building that contains information, and each node is a doorway that leads to a specific part of that information. Navigating this building (also known as traversing the DOM) is done through methods defined in this reference.

Microsoft Internet Controls is what allows you to control IE without ever having to click on the icon and clicking on the window that would appear (also known as creating an instance of IE that can be manipulated programmatically). This object is the backbone to automating IE as it allows you to interact with the web as though you were actually viewing the page inside the browser, but with the key difference in that the computer will just be simulating your actions. There is another object called WebBrowser that can be used, but my initial research seemed to indicate that this object is outdated. Therefore, we will only use the InternetExplorer object found in this reference library.

Microsoft Scripting Runtime provides access to the filesystem (or more generally the folders on your hard drive) of your computer. Though not technically necessary when automating IE, it is useful to be able to open and move files that have been downloaded from the internet.

Step 2 – IE as Class Module

To begin the setup, create a new Class Module and name it something that will help you know that it is an object used for automation (i.e. WebAgent, IE, IEObject, BrowseMeDaily… whatever you want). For this Demo we will refer to it simply as IEAgent. To do this, right-click on your project anywhere within the Project Explorer on the left-hand side of the screen and select Insert > Class Module (highlighted below).

Above - Insert Class Module

Above – Change the Class Module name from Class1 to whatever you want (in this case, IE Agent).

There are many advantages to making IE a class module versus a regular module. The most important reason for making IE a class module is that it allows you to use the same instance of IE across multiple modules (which is important as we will see in subsequent posts). Since we are choosing to make IE a Class Module, we will also be able to use some useful code bites by calling a method found in the Class module instead of having to write that code block every time we use IE. 

Since there are so many reasons for creating a Class Module over a regular module for IE I will not enumerate all of them. But remember this pattern in VBA: if you intend to use something as if it were a standalone object, then it should be a class module; if you want to do something that any object should be able to do (like open a file) then it should be in a module. This is the argument between static and non-static classes in other languages such as Java.

Step 3 – Creating the Basic Structure for Automating IE

This step is where we will actually start writing code. Attached is the module we have written to be found in plaintext form on a shared google doc (blogspot does not support file sharing, so this is a sort of hack). If you wish to skip this step and just download this agent, feel free to do so.

To begin, we must declare some simple variables that will be used in this object, such as the IE object defined in the Microsoft HTML Object Library. Other useful variables will be to define a number variable that will store the process number the IE object will run on. What this means is this: an operating system keeps track of what programs are running by placing each program (or process) in a table and assigning it a number. Whenever we will create this IEAgent, it will create a new IE browser process that the computer will then keep track of by referencing the number it was given upon creation (also known as the process handle). Don’t worry if you don’t understand what this means, just know that it will be used to track the creation of IE in a way that we will be able to interact with it in a different way that will be useful later.

Begin by typing the following:

Dim ie as internetexplorer
Dim handle as Long

You may notice that as you type internetexplorer a popup box appears that changes as you type in the name of the object. This is called intellisense, and is used to help developers determine what objects, properties, or methods are available to them in this context. Though the VBA intellisense is weaksauce compared to other development environments, it is still useful After typing in a few letters after the As you will be able to arrow down to the correct object and press TAB  or Enter.

Enter the following line of code after the variables:

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

This calls what is referred to as an API that will be used to open up webpages in multiple tabs. It is not necessary to this post to understand or even know what this line does, but in essence it will enable us to grab IE as if it was the window we were currently using (called Focus – it determines what window you are writing in/clicking in, etc.). Doing so makes some user functions like hotkeys programmable. So if you want to send a hotkey such as PageDown to IE, this API will be needed to do so.

Next define the following methods:

Private Sub Class_Initialize()
                Set ie = CreateObject("internetexplorer.application")
                handle= ie.hwnd
End Sub
Private Sub Class_Terminate()
                Set ie = Nothing
End Sub

Since IEAgent is a class module, we can run code upon the creation of the IEAgent Object (this is called instantiation, or setting up of object settings). This will both create and destroy the object as necessary.
With this code, you have successfully completed the setup of the IE automation object. The next few posts will be discussing how to flesh out the object to make it do what we want.

No comments:

Post a Comment