新的GMail格式正在抛弃我的Excel VBA代码

时间:2015-11-02 14:22:49

标签: excel vba gmail

GMail最近进行了一些更改,不再支持单页登录格式,而是在登录时切换到逐页登录(至少是我所说的).I& #39;在Excel VBA中测试我的代码,输入电子邮件地址(或用户)并单击"下一步"按钮进入密码页面,然后使用密码页重复该过程。我检查的Excel 2010程序中的引用是:

  • Visual Basic for Applications
  • Microsoft Excel 14.0对象库
  • Microsoft Office 14.0对象库
  • OLE自动化
  • Microsoft Forms 2.0 Library
  • Microsoft Internet Controls
  • Microsoft HTML对象库

填写了电子邮件字段,但之后,编译器抛出运行时错误438并且不会前进。我知道答案可能就在我的鼻子底下,但我似乎无法弄清楚发生了什么。我试过获取"下一个"的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

1 个答案:

答案 0 :(得分:1)

像亚历克斯所说,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

谢谢,我希望这会有所帮助。 :)