VBA – Automate Internet Explorer to Log into a Website Automatically

Ever wanted to automate logging into a website. Well you can automate Internet Explorer to perform such tasks quite easily!

Say you wanted to automate logging into your GMail account at the click of a button, nothing could be easier. Below are 2 different approach to do exactly that:

Function OpenGMail1()
    Dim IE              As Object    'SHDocVw.InternetExplorer

    On Error GoTo Error_Handler
    Set IE = CreateObject("InternetExplorer.Application")

    With IE
        .Navigate "https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/&hl=en"
        .Visible = True    'True/False
        Do While .Busy Or .readyState <> 4: DoEvents: Loop
    End With

    With IE.Document.Forms(0)    'gaia_loginform
        .elements(10).Value = "MyGMailEmailAddress"    'Email
        .elements(11).Value = "MyPassword"    'Passwd
        .elements(12).Click  'signIn
        
        Do While IE.Busy Or IE.readyState <> 4: DoEvents: Loop

        'Continue now that we are logged in
        '...
    End With
    
Error_Handler_Exit:
    On Error Resume Next
    Set IE = Nothing
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred." & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: OpenGMail1" & vbCrLf & _
           "Error Description: " & Err.Description, _
           vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function
Function OpenGMail2()
    Dim IE              As Object    'SHDocVw.InternetExplorer
    Dim HTMLDoc         As Object
    Dim EmailCtl        As Object
    Dim PwdCtl          As Object
    Dim SubmitCtl       As Object

    On Error GoTo Error_Handler
    Set IE = CreateObject("InternetExplorer.Application")

    With IE
        .Navigate "https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/&hl=en"
        .Visible = True    'True/False
        Do While .Busy Or .readyState <> 4: DoEvents: Loop
    End With

    Set HTMLDoc = IE.Document
    Set EmailCtl = HTMLDoc.getelementbyid("Email")
    EmailCtl.Value = "MyGMailEmailAddress"
    Set PwdCtl = HTMLDoc.getelementbyid("Passwd")
    PwdCtl.Value = "MyPassword"
    Set SubmitCtl = HTMLDoc.getelementbyid("signIn")
    SubmitCtl.Click

    Do While IE.Busy Or IE.readyState <> 4: DoEvents: Loop

    'Continue scrapping here
    '...

Error_Handler_Exit:
    On Error Resume Next
    Set SubmitCtl = Nothing
    Set PwdCtl = Nothing
    Set EmailCtl = Nothing
    Set HTMLDoc = Nothing
    Set IE = Nothing
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred." & vbCrLf & vbCrLf & _
            "Error Number: " & Err.Number & vbCrLf & _
            "Error Source: OpenGMail2" & vbCrLf & _
            "Error Description: " & Err.Description, _
            vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

Simple as can be.

These procedures can both be easily adapted to pretty much work with any website by simply changing the Form/Element # or the ElementId for the controls you are trying to bind to.

No Binding To Existing Instances
Typically I would try to bind to an existing instance of IE to automate, as it is more efficient, by doing something along the lines of:

    On Error Resume Next
    Set IE = GetObject(, "InternetExplorer.Application")    'Bind to existing instance of Internet Explorer
    If Err.Number <> 0 Then
        'Could not get instance of IE, so create a new one
        Err.Clear
        Set IE = CreateObject("InternetExplorer.Application")
    End If
    On Error GoTo Error_Handler

Sadly, GetObject() does not work with Internet Explorer Automation and returns:

Run-time error ‘429’:
ActiveX component can’t create object

So there is no need for this block of code and instead we simply simplify things to only use CreateObject, by doing:

    On Error GoTo Error_Handler
    Set IE = CreateObject("InternetExplorer.Application")

I am not a proponent of Internet Explorer (IE) because of major security issues and slower than average performance. That said, sadly, you cannot automate other browsers in such a manner, at least not that I am aware of. So IE does have a serious leg up on the competition when it comes to this type of thing!