Automate Internet Explorer (IE) Using VBA

Associated Files Download Links

This page contains coding examples for automating Internet Explorer (IE) using VBA.

The First piece of code opens IE and navigates to a website. The second piece of code opens IE, navigates to a website and interacts with an input box.

Navigate to a Webpage with VBA

A very common problem people encounter when working with IE in VBA is VBA attempting to run code before Internet Explorer has fully loaded. By using this code, you tell VBA to repeat a loop until IE is ready (IE.ReadyState – 4).

Also, note this line of code:

This code toggles whether IE runs in background or in the foreground.

Open URL and Enter Data in Form Using VBA

alt text


GetElement in IE using VBA

Interacting with objects in Internet Explorer can often be a pain. You need to identify what specific object to work with. In the above code, we are looking for the 3rd instance of “[object HTMLInputElement]” (an input form). Then we enter “orksheet” into the input form (itm.value = “orksheet”), move the cursor into the input form (itm.focus), and type “w”. Typing “w” is necessary in this instance to activate the javascript used to filter the table.

There are more direct methods to selecting objects, however this method should work if all else fails.

To use those other methods you will want to use the following options:

You can run into problems when using these methods if there are more than one element with the same name. By using a loop (as in the sample code above), you can specify which instance of the element to use.

Interact with IE using VBA

In the code above we use the event: Focus (itm.focus) to activate the cursor in the form.

You can find more examples of Object / Element Events, Methods, and Properties here:

Not all of these will work with every object / element and there may be quite a bit of trial and error when interacting with objects in IE.

Sendkeys to Internet Explorer

We used the Sendkeys command in the code above:

Sendkeys should generally be a last resort. You should usually be able to interact with objects directly, however sometimes it’s easier to just use the Sendkeys command. Sendkeys is essentially the same as typing with the keyboard. You need to make sure the correct windows and objects are selecting before proceeding. Sendkeys can also trigger events that run based on user interaction on the web. In the example above, we use Sendkeys to activate the Javascript filter in the Table that we use on the web page.

Sendkeys has two inputs:
1. the key to enter (generally surrounded by {}… {enter}, {q}….)
2. Wait until Sendkeys has completed before proceeding TRUE/FALSE. You will generally want this set to TRUE when working with Internet Explorer.

Run Internet Explorer in Background

To run Internet Explorer in the background you need to do two things:

1. Call the macro containing the IE code with Application.Run so the macro runs in the background as you continue working:

Note: This code could potentially interrupt your work, or your work could interfere with the code. For example, if you use SendKeys, Sendkeys may send a keystroke to the wrong application. Be very careful with this.
2. Hide IE:

VBA Coding Made Easy

Stop searching for VBA code online. Use the Code VBA Add-in to quickly insert your desired code into the Visual Basic Editor.

alt text