Login Gmail using VBA

YasserKhalil
PlatinumLounger
Posts: 4912
Joined: 31 Aug 2016, 09:02

Login Gmail using VBA

Post by YasserKhalil »

Hello everyone
I have this code that is supposed to open my gmail using VBA

Code: Select all

'Reference : Microsoft Internet Controls / Microsoft HTML Object Library

Dim HTMLDoc             As HTMLDocument
Dim MyBrowser           As InternetExplorer

Sub MyGmail()
    Dim MyHTML_Element  As IHTMLElement
    Dim MyURL           As String
    
    On Error GoTo Err_Clear
    MyURL = "https://www.gmail.com"
    
    Set MyBrowser = New InternetExplorer
    MyBrowser.Silent = True
    MyBrowser.navigate MyURL
    MyBrowser.Visible = True

    Do
    Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
    Set HTMLDoc = MyBrowser.document

    'Enter Your Email ID Here
    HTMLDoc.all.Email.Value = "youremail@gmail.com"

    'Enter Your Password Here
    HTMLDoc.all.passwd.Value = "password"

    For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")
        If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
    Next MyHTML_Element

Err_Clear:
    If Err <> 0 Then
        Err.Clear
        Resume Next
    End If
End Sub
It enters the emails only but it doesn't enter the password ... Any idea?

YasserKhalil
PlatinumLounger
Posts: 4912
Joined: 31 Aug 2016, 09:02

Re: Login Gmail using VBA

Post by YasserKhalil »

Thanks a lot
I have figured it out
I add these lines after email id line

Code: Select all

    Do
    Loop Until MyBrowser.readyState = READYSTATE_COMPLETE
    Set HTMLDoc = MyBrowser.document
    
    For Each MyHTML_Element In HTMLDoc.getElementsByTagName("input")
        If MyHTML_Element.Type = "submit" Then MyHTML_Element.Click: Exit For
    Next MyHTML_Element

YasserKhalil
PlatinumLounger
Posts: 4912
Joined: 31 Aug 2016, 09:02

Re: Login Gmail using VBA

Post by YasserKhalil »

After testing again, it failed to enter the password ..!!

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Login Gmail using VBA

Post by HansV »

Do you have a specific reason for wanting to log in to Gmail using VBA?
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4912
Joined: 31 Aug 2016, 09:02

Re: Login Gmail using VBA

Post by YasserKhalil »

Just for curiosity .. I like to check such VBA codes

User avatar
HansV
Administrator
Posts: 78412
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Login Gmail using VBA

Post by HansV »

This is probably difficult. There are two different login pages for Gmail: one in which you have to enter username and password, and another in which you enter only the username and click OK; in the latter you have to provide the password on the next page.
Unless you REALLY need this, I would forget about it.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4912
Joined: 31 Aug 2016, 09:02

Re: Login Gmail using VBA

Post by YasserKhalil »

I have tried once the lines I posted and it worked well. So why worked for once and didn't work later?
Tried to check "Inspect Element" to click "Next" and it worked but for entering the password it didn't work .. although I think it is the same technique