Results 1 to 2 of 2

Thread: Log In To Email Account Using VBA

  1. #1
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10

    Log In To Email Account Using VBA


    Code:
    Option Explicit
    'Reference libraries to be added
    '1. Microsoft HTML Object Library (mshtml.tlb)
    '2. Microsoft Internet Controls (ieframe.dll)
    
    Const strWaitTime As String = "00:00:00"
    Const strURL = "http://www.gmail.com/"
    Const strLoginID As String = "loginID"
    Const strPWD As String = "password"
    Dim strURLMain As String
    Sub GmailLogin()
    'ASSUMPTION: THE HOST SYSTEM HAS INTERNET EXPLORER VERSION 5 OR GREATER INSTALLED
        Dim IE As InternetExplorer
        Dim htmlUserNameTextBox As HTMLTextElement
        Dim htmlUserPswdTextBox As HTMLTextElement
        strURLMain = strURL
        
    ErrH:
        Err.Clear: On Error GoTo -1: On Error GoTo 0: On Error GoTo ErrH
        If Not IE Is Nothing Then
            IE.Quit
            Set IE = Nothing
        End If
        
        'Start the internet explorer
        Set IE = New InternetExplorer
        IE.Visible = True
        'Wait for a while (not necessary, but just in case we want to slow down the frequency of hit to the server) [Ditto wherever used]
        Application.Wait Now() + TimeValue(strWaitTime)
        'Navigate to the main URL
        IE.Navigate strURLMain
        'Hold your horses (so to say) till the page is ready :) [Ditto wherever used]
        IESTATUS IE
        'Pass credentials and click the login / submit button
        Set htmlUserNameTextBox = IE.document.all("Email")
        Set htmlUserPswdTextBox = IE.document.all("Passwd")
        htmlUserNameTextBox.Value = strLoginID
        htmlUserPswdTextBox.Value = strPWD
        IE.document.all("signIn").Click
        IESTATUS IE
    ErH:
        MsgBox Err.Description, vbCritical + vbOKOnly, "Unexpected Error"
        IE.Quit
        
    End Sub
    Private Sub IESTATUS(ByVal IE As InternetExplorer)
        Do While IE.Busy = True: Loop
        Do Until IE.readystate = 4: Loop
        
    End Sub


    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 06-10-2023 at 04:14 PM. Reason: Reference Libraries Comment Given
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  2. #2
    Junior Member
    Join Date
    Dec 2012
    Posts
    12
    Rep Power
    0
    Quote Originally Posted by Excel Fox View Post

    Code:
    Option Explicit
    'Reference libraries to be added
    '1. Microsoft HTML Object Library (mshtml.tlb)
    '2. Microsoft Internet Controls (ieframe.dll)
    
    Const strWaitTime As String = "00:00:00"
    Const strURL = "http://www.gmail.com/"
    Const strLoginID As String = "loginID"
    Const strPWD As String = "password"
    Dim strURLMain As String
    Sub GmailLogin()
    'ASSUMPTION: THE HOST SYSTEM HAS INTERNET EXPLORER VERSION 5 OR GREATER INSTALLED
        Dim IE As InternetExplorer
        Dim htmlUserNameTextBox As HTMLTextElement
        Dim htmlUserPswdTextBox As HTMLTextElement
        strURLMain = strURL
        
    ErrH:
        Err.Clear: On Error GoTo -1: On Error GoTo 0: On Error GoTo ErrH
        If Not IE Is Nothing Then
            IE.Quit
            Set IE = Nothing
        End If
        
        'Start the internet explorer
        Set IE = New InternetExplorer
        IE.Visible = True
        'Wait for a while (not necessary, but just in case we want to slow down the frequency of hit to the server) [Ditto wherever used]
        Application.Wait Now() + TimeValue(strWaitTime)
        'Navigate to the main URL
        IE.Navigate strURLMain
        'Hold your horses (so to say) till the page is ready :) [Ditto wherever used]
        IESTATUS IE
        'Pass credentials and click the login / submit button
        Set htmlUserNameTextBox = IE.document.all("Email")
        Set htmlUserPswdTextBox = IE.document.all("Passwd")
        htmlUserNameTextBox.Value = strLoginID
        htmlUserPswdTextBox.Value = strPWD
        IE.document.all("signIn").Click
        IESTATUS IE
    ErH:
        MsgBox Err.Description, vbCritical + vbOKOnly, "Unexpected Error"
        IE.Quit
        
    End Sub
    Private Sub IESTATUS(ByVal IE As InternetExplorer)
        Do While IE.Busy = True: Loop
        Do Until IE.readystate = 4: Loop
        
    End Sub
    I have encountered this bug
    User-defined type not defined

    at this line
    Code:
    Private Sub IESTATUS(ByVal IE As InternetExplorer

    https://www.youtube.com/channel/UCnxwq2aGJRbjOo_MO54oaHA
    Last edited by DocAElstein; 06-10-2023 at 03:38 PM.

Similar Threads

  1. VBA To Extract Email Address From Text
    By dunndealpr in forum Excel Help
    Replies: 43
    Last Post: 06-05-2019, 03:56 PM
  2. Replies: 2
    Last Post: 05-23-2013, 08:08 AM
  3. Comparing Account Number
    By Howardc in forum Excel Help
    Replies: 6
    Last Post: 03-28-2013, 11:24 PM
  4. How To Send Outlook Email Using VBA
    By mfaisalrazzak in forum Excel Help
    Replies: 7
    Last Post: 03-03-2013, 03:09 AM
  5. Send Lotus Notes Email Using VBA
    By ramakrishnan in forum Excel Help
    Replies: 1
    Last Post: 09-08-2011, 09:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •