新的 GMail 格式使我的 Excel VBA 代码失效

New GMail format is throwing off my Excel VBA code

GMail最近发生了一些变化,不再支持一页登录格式,而是在登录时切换为逐页登录(至少我是这么说的)。我在 Excel VBA 中测试我的代码,输入电子邮件地址(或用户)并单击 "Next" 按钮前进到密码页面,然后在密码页面重复该过程。我的 Excel 2010 计划中被勾选的参考文献是:

email字段填好了,但是填完之后,编译器抛出运行时错误438,不会前进。我知道答案可能就在我眼皮底下,但我似乎无法弄清楚发生了什么。我尝试获取 "Next" 按钮的 HTML ID,但无济于事。我只是卡住了。

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 = "xxxxxxxxxxx@gmail.com"
    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

正如 Alex 所说,API 路线是最快和更好的路线。但是试试这个代码。

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 = "myemail@gmail.com"
    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

谢谢,希望对您有所帮助。 :)