How to Login Automatically into Website Using Excel VBA

Home More Excel (VBA) Training Videos

Alex wants to know how to login into a website utomatically 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 this training video on youtube




Home More Excel (VBA) Training Videos