0
votes

GMail recently underwent some changes and no longer supports the one-page login format, and instead has switched to a page-by-page step login (at least that's what I call it) when logging in. I'm testing my code out in Excel VBA which inputs the email address (or user) and to click the "Next" button to advance to the password page, then repeats the process with the password page. The references in my Excel 2010 program which are checked off are:

  • Visual Basic for Applications
  • Microsoft Excel 14.0 Object Library
  • Microsoft Office 14.0 Object Library
  • OLE Automation
  • Microsoft Forms 2.0 Library
  • Microsoft Internet Controls
  • Microsoft HTML Object Library

The email field is filled in, but after that, the compiler throws the runtime error 438 and won't advance. I know the answer is probably right under my nose, but I can't seem to figure out what's going on. I have tried getting the HTML ID of the "Next" button, but to no avail. I'm just stuck.

Option Explicit
Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer

Sub MyGmail()
    Dim MyHTML_Element As IHTMLElement
    Dim MyURL As String

    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
    HTMLDoc.all.Email.Value = "[email protected]"
    HTMLDoc.all.passwd.Value = "xxxxxxxxxxxxxx"
    For Each MyHTML_Element In HTMLDoc.getElementsByID("Email")
        If MyHTML_Element.ID = "next" Then MyHTML_Element.Click: Exit For
    Next
Err_Clear:
    If Err <> 0 Then
        Err.Clear
        Resume Next
    End If
End Sub
1
Thank you. I know this, but I'm just trying to figure out the code for going into GMail through Excel. I'm testing out Excel macros for a small business, and I want to be able to go through the multi-step process to login without having to use APIs. - Tak

1 Answers

1
votes

Like Alex said the API route would be the fastest and better way to go. But give this code a try.

Option Explicit

Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer

Sub MyGmail()

    Dim MyHTML_Element As IHTMLElement
    Dim MyURL As String
    Dim oSignInLink As HTMLLinkElement
    Dim oInputEmail As HTMLInputElement
    Dim oInputNextButton As HTMLInputButtonElement
    Dim oInputPassword As HTMLInputElement
    Dim oInputSigninButton As HTMLInputButtonElement

    MyURL = "https://www.gmail.com"

    Set MyBrowser = New InternetExplorer

    ' Open the browser and navigate.
    With MyBrowser
        .Silent = True
        .Navigate MyURL
        .Visible = True
        Do
            DoEvents
        Loop Until .ReadyState = READYSTATE_COMPLETE
    End With


    ' Get the html document.
    Set HTMLDoc = MyBrowser.Document

    ' See if you have the sign in link is because you are in the main
    ' page
    Set oSignInLink = HTMLDoc.getElementById("gmail-sign-in")
    If Not oSignInLink Is Nothing Then
        oSignInLink.Click
        Do
            DoEvents
        Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE
    End If

    ' Get the email field and the next button
    Set oInputEmail = HTMLDoc.getElementById("Email")
    Set oInputNextButton = HTMLDoc.getElementById("Next")

    ' Click the button and wait
    oInputEmail.Value = "[email protected]"
    oInputNextButton.Click
    Do
        DoEvents
    Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE

    ' Get the password field and the sign in button
    Set oInputPassword = HTMLDoc.getElementById("Passwd")
    Set oInputSigninButton = HTMLDoc.getElementById("signIn")


    ' Click the button and wait
    oInputPassword.Value = "mypassword"
    oInputSigninButton.Click
      Do
        DoEvents
    Loop Until MyBrowser.ReadyState = READYSTATE_COMPLETE

Err_Clear:
    If Err <> 0 Then
        Err.Clear
        Resume Next
    End If

End Sub

Thanks I hope this helps. :)