2014-12-04 19 views
0

我試圖登錄到使用IE作爲瀏覽器的電子郵件帳戶,這是所有使用的過人之處VBA環境實現自動化,到目前爲止,我有以下幾點:IE登錄自動化 - 如何按提交按鈕?

Option Explicit 

Sub login() 
    Const cURL = "https://login.microsoftonline.com/" 
    Const cUsername = "XXXX" 'REPLACE XXXX WITH YOUR USER NAME 
    Const cPassword = "YYYY" 'REPLACE YYYY WITH YOUR PASSWORD 

    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 

    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 
    Set UserNameInputBox = LoginForm.elements("cred_userid_inputtext") 
    UserNameInputBox.Value = cUsername 

    'Get the password textbox and populate it 
    Set PasswordInputBox = LoginForm.elements("cred_password_inputtext") 
    PasswordInputBox.Value = cPassword 

    'Get the form input button and click it 
    Set SignInButton = LoginForm.elements("cred_sign_in_button") 
    SignInButton.Click 

    'Wait for the new page to load 
    Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop 

    Debug.Print "Current URL: " & IE.LocationURL 

End Sub 

使用此代碼,用戶名和密碼輸入正常,但當我嘗試按下「提交」按鈕時出現問題,我得到一個運行時錯誤,並不知道爲什麼,但我認爲它可能是一個JavaScript對象,但無法弄清楚如何讓VBA按下它。任何幫助,將不勝感激。

+0

只需使用'LoginForm.Submit' – 2014-12-04 20:28:04

+0

你能爲提交按鈕提供相關的HTML代碼?什麼運行時錯誤正在發生? – cheezsteak 2014-12-04 20:37:39

回答

0

如果LoginForm項目是一個真正的html'form'元素,那麼它應該有一個提交方法。在這種情況下,LoginForm.Submit會做的伎倆

+0

謝謝,我現在進來了。你有任何想法如何使用vba在Microsoft 365內創建新的電子郵件? – 2014-12-04 21:45:22

+0

我這樣做 - 但這需要作爲單獨的問題發佈,因爲它與此線程無關。 :) – 2014-12-05 09:29:32

+0

我創建了以下問題:http://stackoverflow.com/questions/27305885/press-new-mail-button-in-microsoft-365-email-using-vba?noredirect=1#comment43074433_27305885在您的幫助這是非常感謝! – 2014-12-05 10:18:53