Automate Internet Explorer (IE) Using VBA



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: https://msdn.microsoft.com/en-us/library/ms535893(v=vs.85).aspx

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


<<Return to VBA Examples

Did you find this VBA tutorial useful? Then share it with your friends and colleagues:

9 Comments

  1. Kars June 3, 2017 at 11:35 am - Reply

    I have not tested the code but it its worderfully commented and explained. Thank you so much!
    I would like to automatically fill in an online calculator, but the calculator is available after log on and a few hyperlinks. Is there a form to set and already loaded website as an object?

    ‘Get Window ID for IE so we can set it as activate window
    HWNDSrc = IE.HWND
    ‘Set IE as Active Window
    SetForegroundWindow HWNDSrc

    I know it must be related to this part of your code, but I am not sure how to proceed.

  2. Alfredo Reynoso November 13, 2017 at 3:43 pm - Reply

    Hi,

    I hope you are well, I would like to know what is the commplement taht you use in your VBA.

    Regards
    Alfredo Reynoso

    • Steven Rynearson November 13, 2017 at 4:12 pm - Reply

      Hi Alfredo – What do you mean exactly?
      -Steve

  3. Avanit November 21, 2017 at 12:57 pm - Reply

    How could I get the object type for Rumba Mainframe……Or Could you please share the code for connecting Rumba Mainframe through VBA.

  4. Martin December 31, 2017 at 3:20 am - Reply

    I cann’t get the first example to run. I got a 429 error – active x can’t create object. How is this possible since Internet Explorer comes with O.S? I’m running a new Office 2016 Excel install. I must admit I am a loss to understand how the create object resolves down to the path name of the application c:\Program Files\Internet Explorer\iexplore.exe What if I wanted to launch a different web browser like Firefox?

  5. Martin Noyes January 3, 2018 at 12:53 am - Reply

    I’m new to VBA and rusty on the old standalone VB so please forgive me if I ask stupid questions.

    1. Your first example: ‘Create InternetExplorer Object
    Set IE = CreateObject(“InternetExplorer.Application”)
    How does this get translated into the exact path to the iexplore.exe executable? What if I wanted to use Chrome or Firefox? Would “”Firefox.Application” magically find the executable?

    2. “‘IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
    Do While IE.ReadyState = 4: DoEvents: Loop ‘Do While
    Do Until IE.ReadyState = 4: DoEvents: Loop ‘Do Until”
    How do you know that the ready state for IE is decimal 4? Where does one look that up? Would it be the same for other browsers? And what other parameters are defined for browsers and where can they be found?

    Thanks for a great article. Very enlightening. And I agree with you about the SendKeys. Tried it in a macro and what a self destroying disaster it was. The object (a calculator) immediately lost focus and the macro started writing all over itself. Keeping the focus on the object looks like a major problem.

    Thanks Again.

    • Steven Rynearson January 14, 2018 at 10:48 pm - Reply

      Hi sorry for the late reply.

      1. I’ve never tried to use Chrome or Firefox. Internet Explorer is Microsoft software so it works well with VBA. You would use a similar method to open PowerPoint, Word, etc. (other Microsoft software). Unless you have a good reason, I would stick to IE if you’re trying to use VBA.

      2. Every browser has “Developer Tools” or something similar that lets you dive into the code of how a website loads. I’m not sure if you can find the ReadyState there, but there is other useful information.

      -Steve

    • Rm February 6, 2018 at 11:57 am - Reply

      Hi Martin.

      There is no such extension for VBA . The thing you are asking how ie magically appear in VBA is called a reference.
      Firefox and chrome doesnt have their references for use with VBA.

Leave A Comment