How to Login Automatically into Website Using Excel VBA

Alex wants to know how to login into a website automatically using Excel VBA. His email is reproduced below:
‘Hi Mr.Takyar,

Thank you for providing such valuable tutorials for excel.

I was wondering if there is a way this task can be accomplished.

I’m trying to access a website (URL) that requires username and password.

I have to enter my credentials every time I access this site. Is there a way to create a macro or VBA that I can run to make the process quicker? If so can you please demonstrate how I can get this accomplished. I sincerely appreciate your help.

Alex’

He wants to automate the process of logging into a website automatically using Excel VBA. He has the username and password for the website. Our video demonstrates how you can login automatically into a website like Gmail using Excel VBA:
1. Go to the Gmail website
2. View its source code to find out the labels used for the username and password
3. Start the Visual Basic Editor in MS Excel
4. Activate the following two items by clicking on Tools and then ‘References…’:
•Microsoft Internet Controls
•Microsoft HTML Object Liabrary
5. Next define two public variables
6. Now start writing the macro
7. Using a looping process access the username and password text-boxes automatically for entry of the relevant data
8. Then identify the ‘submit’ button, click on it and exit the for loop
9. To avoid error messages popping up write a few lines of code to ignore the errors
You’ll notice that the VBA code given below works well:

Dim HTMLDoc As HTMLDocument
Dim MyBrowser As InternetExplorer
Sub MyGmail()

Dim MyHTML_Element As IHTMLElement
Dim MyURL As String
On Error GoTo Err_Clear
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 = “dinesh.takyar@gmail.com” ‘Enter your email id here
HTMLDoc.all.passwd.Value = “abc+123” ‘Enter your password here
For Each MyHTML_Element In HTMLDoc.getElementsByTagName(“input”)
If MyHTML_Element.Type = “submit” Then MyHTML_Element.Click: Exit For
Next
Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If
End Sub

Watch the video:


27 thoughts on “How to Login Automatically into Website Using Excel VBA

      1. Nuzul

        Can i know which path Shell (“C:\Program Files (x86)\Mozilla Firefox\firefox.exe should i input it?

        This one?
        Dim MyBrowser As InternetExplorer
        Set MyBrowser = New InternetExplorer

        Reply
  1. Shilpi

    Hi Sir, I am trying to run this for my Gmail ID; it works all good till putting in the email and password. But, it stops there, doesn’t automatically click on the ‘sign in’. It is very urgent sir, I am working on a project that needs to be submitted in 4 days. Please help.

    Reply
      1. Vivek

        But in this case, we are not entering any value; we are clicking in so how can this work. Can you guide mr dinesh. Clicking code has to be different

        Reply
      2. Kiran Gondkar

        Hi Dinesh Sir

        I am too facing same issue.. i have checked ‘Sign In ‘ everything is fine but still facing same issue.

        Can you please fix this problem???thanks in advance..

        Reply
  2. Terry Thornton

    How do we use this to convert a worksheet to a html file and then up load it to our web site?

    Thank you Dinesh.

    Reply
  3. Glen

    Dear sir
    It shows a compile error
    the below lines of code is highlighted in “red” when i paste it please help
    MyURL = “https://www.gmail.com”
    HTMLDoc.all.Email.Value = “glynsonb@gmail.com” ‘Enter your email id here
    HTMLDoc.all.passwd.Value = “abc123″ ‘Enter your password here

    Reply
      1. Ritesh

        This is because of the “” used in the code. Please remove the “” written in code and replace it manually in your system. The code has the “” in other format. I hope this will help u.

        Reply
  4. Mahendra

    How can i browse and login to a website using firefox, in IE it is working fine but i want it in Mozilla as Application is compatible with mozilla.. Please help here

    Reply
  5. Felix Falconi

    Nice code !!
    But I can do to open in url than email. (other non-email)
    for example this page,
    http://www.exceltrainingvideos.com/how-to-login-automatically-into-website-using-excel-vba/

    or other blogs

    I guest
    I need username and password.
    In the Excel would have to A2: the url, B2: user name, C2: key (this because eventually can change it). I click on a command button and automatically open my page. As I can do this with Excel macro code?
    Thank you

    Reply
  6. Pingback: ユーザー(USER) 水で貼る結露防止シート(45×180cm 2枚組) U-225 スノー 【送料無料】【02P30M

  7. Pingback: How to follow multiple hyperlinks and extract webpage data | Excel VBA Training Videos

  8. Vikas Verma

    Hi Thanks for creating such useful macros and I need a micro for auto web login and I was try this code but unfortunately its not working its showing error at time of Compile in bellow mention line.
    HTMLDoc.all.Email.Value = “dinesh.takyar@gmail.com” ‘Enter your email id here
    HTMLDoc.all.passwd.Value = “abc+123” ‘Enter your password here

    I think my system is not recognize (Email) function so please suggest in this error msg.

    I really appreciate your effort.

    Thank You SO much.

    Reply
    1. Vikas Verma

      Hi Sir,

      I need your help everything is working fine in above code but its not submitting for login so need your help its Opening Website – Entering ID & Password but its not clicking on login button and I was also try by website source code so please help me out.

      Thanks in Advance 🙂

      Reply
    1. Kiran Gondkar

      just go to Tools > References > check in for 1.Microsoft HTML Object Library 2. Microsoft Internet Controls

      Reply
  9. parvinder Singh

    Hello Sir,

    How do I clear the existing data present under Email address box and enter new data .

    For example, I’m working on Automating and raising a Ticket and I’ve saved a Quick link for that.

    The quicklink for that ticketing website isn’t being saved unless I enter some text in it.

    So, I wrote “test” under description section of the Ticket. I’m trying to pull the data from Excel Cells, and pasting it under description.

    But the text isnt being pasted under description section and it says “test “even after macro is run successfully. I’ve inspected the element and I’m looking at correct section.

    However, any data isn’t being pasted in the other boxes of the ticket as well. Please advise sir, below is my code:

    Dim HTMLDoc As HTMLDocument
    Dim MyBrowser As InternetExplorer
    Sub TT()
    Dim MyURL As String
    On Error GoTo Err_Clear
    MyURL = ” CONFIDENTIAL SITE”
    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.short_description.Value = “TIGER” HTMLDoc.all.Details.Value = “LION” For Each MyHTML_Element In HTMLDoc.getElementsByTagName(“input”) If MyHTML_Element.Type = “submit” Then MyHTML_Element.Click: Exit For Next Err_Clear: If Err 0 Then Err.Clear Resume Next End If End Sub

    Reply
  10. Josh

    I am trying to get this to work . So far with your code I am able to get the uid and pw typed into the page correctly but cant click the sign in button.
    Here is the html from the page.
    Sign In

    And here is my code
    Dim HTMLDoc As HTMLDocument
    Dim MyBrowser As InternetExplorer

    Sub MyBoa()

    Dim MyHTML_Element As IHTMLElement
    Dim MyURL As String
    On Error GoTo Err_Clear
    MyURL = “https://www.bankofamerica.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.onlineId1.Value = “myuidwashere”
    HTMLDoc.all.passcode1.Value = “mypwwashere”

    For Each MyHTML_Element In HTMLDoc.getElementsByTagName(“input”)
    If MyHTML_Element.Type = “Sign in” Then MyHTML_Element.Click: Exit For
    Next
    Err_Clear:
    If Err 0 Then
    Err.Clear
    Resume Next
    End If
    End Sub

    Any ideas I would love to get this to function fully!

    Reply
    1. Josh

      let me try the html code again since it ran the code instead of showing it.

      button id=”hp-sign-in-btn” class=”hp-sign-in-btn btn-bofa btn-bofa-small btn-bofa-noRight btn-bofa-blue” data-cm='[“homepage_sign_in”, “homepage:Content;home_personal”]’>Sign In</button

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *