新的GMail格式是抛出我的Excel VBA代码

GMail最近进行了一些更改,不再支持单页login格式,而是login时切换到逐页login(至less这就是我所说的)。我正在testing我的代码input电子邮件地址(或用户)并单击“下一步”button进入密码页面的Excel VBA,然后用密码页面重复该过程。 在我的Excel 2010程序中被检查的参考是:

  • Visual Basic for Applications
  • Microsoft Excel 14.0对象库
  • Microsoft Office 14.0对象库
  • OLE自动化
  • Microsoft Forms 2.0库
  • 微软互联网控制
  • Microsoft HTML对象库

电子邮件字段被填充,但之后,编译器会抛出运行时错误438,并且不会前进。 我知道答案可能是在我的鼻子下,但我似乎无法弄清楚发生了什么事情。 我试图得到“下一步”button的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 

谢谢,我希望这有助于。 🙂