April 3, 2018

How to Log into Gmail Automatically

How to Log into Gmail Automatically with VBA. Google has changed its Gmail interface completely. Instead of opening gmail.com, we go directly to the login page of the URL.
Watch the video:

Watch the video on YouTube.

Here’s the complete VBA code to log into your Gmail account automatically:

Sub Login_Gmail()

‘Set a reference (Visual Basic Editor) > Tools > References) to the following libraries:
‘ a) Microsoft Internet Controls
‘ b) Microsoft HTML Object Library

Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim HTMLElement As MSHTML.IHTMLElement

With IE
.Visible = True
.Silent = True ‘avoid any pop-up
.navigate “https://accounts.google.com/signin/v2/identifier?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin”
Do While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With

Call myTimer

Set HTMLDoc = IE.document

HTMLDoc.all.identifier.Value = “[email protected]
HTMLDoc.all.identifierNext.Click

With IE
Do While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With

Call myTimer

For Each HTMLElement In HTMLDoc.getElementsByName(“password”)
If HTMLElement.getAttribute(“type”) = “password” Then
‘enter the password for your account
HTMLElement.Value = “enteryourpassword”
Exit For
End If
Next HTMLElement

HTMLDoc.all.passwordNext.Click

Set IE = Nothing
Set HTMLDoc = Nothing
Set HTMLElement = Nothing

End Sub

Private Sub myTimer()

Dim timerStart As Single

Const pauseTIME As Integer = 5 ‘seconds

timerStart = Timer
Do Until Timer – timerStart > pauseTIME
DoEvents
Loop

End Sub

Further Reading:

How to automate login to Gmail

7 thoughts on “How to Log into Gmail Automatically

  1. Thanks pal!! Simple and concrete… you can avoid the “Sleep” with timer using this command:

    Application.wait Now + TimeValue(“00:00:01”) ‘ Seconds

  2. I want to thank you in advance for showing me how to fix the following compiler errors, that were highlighted in red, when I tried to run the vba code from excel 2016. I want to use your vba code to open gmail automatically in excel 2016 on a windows 10 laptop.

    I am using google chrome as my browser. I am also using your vba module “send_email_via_Gmail()” to send gmail email with attachments from excel and it works great.
    Thank you …… Sherwood Lemoine
    ‘ compiler errors as follows
    .Silent = True ‘avoid any pop-up

    .navigate “https://accounts.google.com/signin/v2/identifier?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin”
    HTMLDoc.all.identifier.Value = “[email protected]
    HTMLDoc.all.identifier.Value = “[email protected]

    1. update on error:
      I corrected all the errors in your code that was caused by the wrong vba comment symbol (‘) when I copied your code.

      Sherwood Lemoine 6/4/2019 10:48 AM:
      The only error that I am getting now is in “Private Sub myTimer()” on the following line of code:
      Do Until Timer – timerStart > pauseTIME

      Please let me know how to fix the error. I am using chrome and not IE.
      Thanks … Sherwood

      1. Use this: It works for me.
        Dim PauseTime

        PauseTime = 3
        timerStart = Timer

        Do While Timer < timerStart + PauseTime

  3. please update the same code to 64 bit for windows 10. This code is not working in 64 bit.

Comments are closed.