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: