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:


17 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
  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

Leave a Reply

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