通过VBA自动登录Gmail

时间:2019-05-24 19:15:07

标签: excel vba

我搜索了自动登录的Gmail帐户,该帐户返回的帖子为零。

寻找将自动登录到我的Gmail帐户的VBA宏。互联网上有很多示例,但它们根本不起作用。最好的情况是,示例代码将我带到了USERNAME,但它没有应用密码并打开了我的Gmail帐户。

有帮助吗?感谢您的所有帮助。

Sub Login_Gmail()

‘Set a reference (Visual Basic Editor) > Tools > References) to the following libraries:
‘ a) Microsoft Internet Controls
‘ b) Microsoft HTML Object Library

Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As New MSHTML.HTMLDocument
Dim HTMLElement As MSHTML.IHTMLElement

With IE
.Visible = True
.Silent = True ‘avoid any pop-up
.navigate “https://accounts.google.com/signin/v2/identifier?continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&service=mail&sacu=1&rip=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin”
Do While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With

Call myTimer

Set HTMLDoc = IE.document

HTMLDoc.all.identifier.Value = “myaccount@gmail.com”
HTMLDoc.all.identifierNext.Click

With IE
Do While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Loop
End With

Call myTimer

For Each HTMLElement In HTMLDoc.getElementsByName(“password”)
If HTMLElement.getAttribute(“type”) = “password” Then
‘enter the password for your account
HTMLElement.Value = “enteryourpassword”
Exit For
End If
Next HTMLElement

HTMLDoc.all.passwordNext.Click

Set IE = Nothing
Set HTMLDoc = Nothing
Set HTMLElement = Nothing

End Sub

Private Sub myTimer()

Dim timerStart As Single

Const pauseTIME As Integer = 5 ‘seconds

timerStart = Timer
Do Until Timer – timerStart > pauseTIME
DoEvents
Loop

End Sub

另一个例子(如果可以的话,我会更喜欢)

Sub Test()

    Const cURL = "http://mail.google.com" 'Enter the web address here
    Const cUsername = "XXXXXX" 'Enter your user name here
    Const cPassword = "XXXXXX" 'Enter your Password here

    Dim IE As InternetExplorer
    Dim doc As HTMLDocument
    Dim LoginForm As HTMLFormElement
    Dim UserNameInputBox As HTMLInputElement
    Dim PasswordInputBox As HTMLInputElement
    Dim SignInButton As HTMLInputButtonElement
    Dim HTMLelement As IHTMLElement
    Dim qt As QueryTable

    Set IE = New InternetExplorer

    IE.Visible = True
    IE.Navigate cURL

    'Wait for initial page to load

    Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop

    Set doc = IE.Document

    'Get the only form on the page

    Set LoginForm = doc.forms(0)

    'Get the User Name textbox and populate it
    'input name="Email" id="Email" size="18" value="" class="gaia le val" type="text"

    Set UserNameInputBox = LoginForm.elements("Email")
    UserNameInputBox.Value = cUsername

    'Get the password textbox and populate it
    'input name="Passwd" id="Passwd" size="18" class="gaia le val" type="password"


    Set PasswordInputBox = LoginForm.elements("Passwd")
    PasswordInputBox.Value = cPassword

    'Get the form input button and click it
    'input class="gaia le button" name="signIn" id="signIn" value="Sign in" type="submit"

    Set SignInButton = LoginForm.elements("signIn")
    SignInButton.Click

    'Wait for the new page to load

    Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop

    End Sub

0 个答案:

没有答案