Consulting

Results 1 to 2 of 2

Thread: Can you use vba to log on to a website?

  1. #1
    Site Admin VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,495
    Location

    Can you use vba to log on to a website?

    In reviewing some of the older threads in the forum, I came across one that suggested it may be possible, so I started thinking....

    Sub LoginToWebsite()
        ' Declare variables
        Dim IE As Object ' Represents the Internet Explorer application
        Dim doc As Object ' Represents the HTML document within IE
        Dim element As Object ' Represents a generic HTML element
        Dim url As String ' The URL of the login page
        Dim usernameFieldID As String ' The ID of the username input field
        Dim passwordFieldID As String ' The ID of the password input field
        Dim loginButtonID As String ' The ID of the login button (or name/tag)
        Dim username As String ' Your username
        Dim password As String ' Your password
        ' --- Configuration ---
        ' IMPORTANT: Replace these placeholders with the actual values for your target website.
        ' You'll need to inspect the website's HTML source code to find these IDs/names.
        url = "https://www.example.com/login" ' Replace with the actual login page URL
        usernameFieldID = "username" ' Replace with the actual ID of the username input field
        passwordFieldID = "password" ' Replace with the actual ID of the password input field
        loginButtonID = "loginButton" ' Replace with the actual ID of the login button (or name/tag)
        username = "your_username" ' Replace with your actual username
        password = "your_password" ' Replace with your actual password
        ' --- End Configuration ---
        On Error GoTo ErrorHandler
        ' Create a new Internet Explorer object
        Set IE = CreateObject("InternetExplorer.Application")
        ' Make IE visible (set to False if you want it to run in the background)
        IE.Visible = True
        ' Navigate to the login page
        IE.Navigate url
        ' Wait for the page to load completely
        ' Loop until the document is ready state 4 (complete)
        Do While IE.ReadyState <> 4 Or IE.Busy
            DoEvents
        Loop
        ' Set a reference to the HTML document
        Set doc = IE.Document
        ' Find the username input field and enter the username
        Set element = doc.getElementById(usernameFieldID)
        If Not element Is Nothing Then
            element.Value = username
        Else
            MsgBox "Username field with ID '" & usernameFieldID & "' not found.", vbCritical
            IE.Quit
            Set IE = Nothing
            Exit Sub
        End If
        ' Find the password input field and enter the password
        Set element = doc.getElementById(passwordFieldID)
        If Not element Is Nothing Then
            element.Value = password
        Else
            MsgBox "Password field with ID '" & passwordFieldID & "' not found.", vbCritical
            IE.Quit
            Set IE = Nothing
            Exit Sub
        End If
        ' Find and click the login button
        ' This part might vary depending on whether the button has an ID, Name, or is a specific tag.
        ' Try getElementById first. If not, you might need to use getElementsByName or getElementsByTagName.
        Set element = doc.getElementById(loginButtonID)
        If Not element Is Nothing Then
            element.Click
        Else
            ' If not found by ID, try by name (common for buttons)
            Set element = doc.getElementsByName(loginButtonID).Item(0)
            If Not element Is Nothing Then
                element.Click
            Else
                ' If not found by ID or Name, try by tag name (e.g., "input" or "button")
                ' This is less specific and might require looping through elements
                ' For example, to find a button with specific text:
                ' For Each element In doc.getElementsByTagName("input")
                '     If element.Type = "submit" And element.Value = "Log In" Then
                '         element.Click
                '         Exit For
                '     End If
                ' Next element
                ' MsgBox "Login button with ID/Name '" & loginButtonID & "' not found. Manual intervention needed.", vbCritical
                MsgBox "Login button with ID/Name '" & loginButtonID & "' not found. Please check the ID/Name or adjust the code to find the button.", vbCritical
                IE.Quit
                Set IE = Nothing
                Exit Sub
            End If
        End If
        ' Wait for the page to load after login (e.g., the dashboard page)
        Do While IE.ReadyState <> 4 Or IE.Busy
            DoEvents
        Loop
        ' Optional: Display a message box to confirm successful login or show the current URL
        MsgBox "Login process completed. Current URL: " & IE.LocationURL, vbInformation
        ' Clean up
        Set doc = Nothing
        Set IE = Nothing
        Exit Sub
        ErrorHandler:
        MsgBox "An error occurred: " & Err.Description, vbCritical
        If Not IE Is Nothing Then
            IE.Quit
            Set IE = Nothing
        End If
    End Sub
    Would the above be close or not?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  2. #2
    VBAX Mentor
    Joined
    Nov 2022
    Location
    The Great Land
    Posts
    443
    Location
    I have done it.

    Did you test that code? Looks good, if you still have IE installed.

    If you know element names, code can be much simpler.

    Sample from my db:
            'open ASTM/AASHTO test standards website and pass agency username/password to the web page
            Dim oBrowser As InternetExplorer
            Set oBrowser = New InternetExplorer
            oBrowser.Silent = True
            oBrowser.Navigate "https://login.ihserc.com/login/erc?"
            oBrowser.Visible = True
            Do
                'Wait till the Browser is loaded
            Loop Until oBrowser.ReadyState = READYSTATE_COMPLETE
            oBrowser.Document.all.Item("subAcctLoginName").Value = "username"
            oBrowser.Document.all.Item("subAcctPassword").Value = "password"
            oBrowser.Document.all.Item("Submit").Click
    Might find this of interest https://learn.microsoft.com/en-us/an...ge-through-vba
    Last edited by June7; 07-08-2025 at 07:59 PM.
    How to attach file: Reading and Posting Messages (vbaexpress.com), click Go Advanced below post edit window. To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Posting Permissions

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