Saturday, August 08, 2009

How to Login to Website Using VBA

Login to Google Account using VBA


Here is a simple code that will login to Google accounts with the provided user-name and password.



The program requires references to the following:

1 Microsoft Internet Controls
2. Microsoft HTML Object Library

Microsoft HTML Object LibraryMicrosoft HTML Object Library- VBA Reference

Microsoft Internet Controls - VBA Reference Microsoft Internet Controls - VBA Reference



The Internet control is used to browse the webpage and the HTML Objects are used to identify the username and password textboxes and submit the text using the control button.


Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()


Dim oHTML_Element As IHTMLElement
Dim sURL As String


On Error GoTo Err_Clear
sURL = "https://www.google.com/accounts/Login"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True

Do
' Wait till the Browser is loaded
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

Set HTMLDoc = oBrowser.Document

HTMLDoc.all.Email.Value = "
sample@vbadud.com"
HTMLDoc.all.passwd.Value = "*****"

For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next


' oBrowser.Refresh ' Refresh If Needed
Err_Clear:
If Err <> 0 Then
Debug.Assert Err = 0
Err.Clear
Resume Next
End If

End Sub






See Also : http://vbadud.blogspot.com/2008/05/google-search-using-vba.html

15 comments:

  1. Justin8:28 AM

    Good morning. I am a very amatuer coded but have navigated through this script well so far. I am having issues identifying the "object" or whatever I'm suppose to reference to enter a password on https://intex.com/main/login.php. The issue in the code that I'm having is at the HTMLDoc.all.Email.Value = and the HTMLDoc.all.Password.Value = lines of code. Could you look at the source of my target URL and let me know what I need to change? Thanks so much for your help.

    ReplyDelete
  2. Anonymous12:19 PM

    I had the same problem.
    Copy the procedure code to a Public Sub in a module rename it subTEST. I use modTEST as a name for the module.

    Substitute this:
    For Each oHTML_Element In HTMLDoc.getElementsByTagName("input")
    Debug.Print oHTML_Element.Name
    Next

    In the immediate window run subTEST.
    The names of the fields should print. Try them all till you find the one that works.

    ReplyDelete
  3. Anonymous5:21 AM

    On this line code breaks:
    Debug.Assert Err = 0

    It's an unnecessary line, that duplicates the one above it, but for contrary condition. I recommend erasing it.

    ReplyDelete
  4. Anonymous8:29 PM

    Hi,
    This tutorial is awesome; this really helped me a lot.
    I’ve a question: How do I click on a specified link (Hyperlink) to go to next page or do next task?
    The way it is given for submit button.
    Thanks

    ReplyDelete
  5. Anonymous5:56 PM

    Hey everyone I fond this code and used it. Thus far it works like a charm to connect to Google. Also I agree if you were to take Debug.Assert Err = 0 part out the code works perfect and you no longer get the annoying error.

    Now I am trying the same code to connect to another webpage and for some reason its not inputting the my usename or my password in the required field. This is what I mean to say

    'Now wait untill internet explorer is ready

    Loop Until oBrowser.readyState = ReadyState_Complete

    Set HTMLDoc = oBrowser.Document

    HTMLDoc.all.UserName.Value = " "
    HTMLDoc.all.Password.Value =" "

    I am leaving the fields blank. But everytime I run the code I get to the desired webpage but it doesnt put in my info as requested in the code.

    Can some one please help me :)

    ReplyDelete
  6. Anonymous12:29 PM

    Hi All,
    Im ahving the same issue. I know this is an old post so i dont expect much of an answer.
    *******************

    Set HTMLDoc = oBrowser.Document

    HTMLDoc.all.UserName.Value = " "
    HTMLDoc.all.Password.Value =" "
    ********************

    for some reason the HTMLDoc is comming back as "Nothing"???

    therefore its not inputting my used name or password.

    any assistance would be greatfull

    ReplyDelete
  7. Ulisses (Fortaleza/Brasil)6:28 AM

    I'd like to thank you, even at Oct/2012 this post keeps helping people!

    ReplyDelete
  8. Anonymous10:36 AM

    Did anyone have any security issues after using this code? Any stolen password or account?

    ReplyDelete
  9. Is there a way to get those "username" and "password" names from table or form. I mean not the values of "username" and "password" but the names of those fields in HTML.
    What I want to do is to use one universal script to log to many different sites. The only difference will be the names of "username" and "password" fields in HTML of the site. So I want to write something like this:
    HTMLDoc.all.FORM!CONTROL.Value = " "

    ReplyDelete
  10. Anonymous7:57 AM

    hey, i tried the code and i get this error:

    "The object invoked has disconnected from its clients "

    At line:
    Loop Until oBrowser.readyState = READYSTATE_COMPLETE

    Can some1 help me?

    ReplyDelete
  11. Anonymous5:05 AM

    i need an extra step to be added to this code is :

    search a text / string on the web page which is the proof that web page is loaded completely and update the excel cell with Good

    ReplyDelete
  12. sir,

    i have three credentials to enter into wesite they are
    login
    password and
    client code

    please help me to login automatically

    ReplyDelete
  13. function decodeHTML(value)
    {
    var element = document.createElement('div');
    element.innerHTML = element.textContent = value;
    value = element.innerText || element.textContent;
    delete element;
    return value;
    The above code is written in sourcecode can we use element collection, element name I could not use this

    ReplyDelete
  14. WHERE CAN I GET THE PROGRMS FOR THESE...

    1 Microsoft Internet Controls
    2. Microsoft HTML Object Librar

    ReplyDelete
  15. I would like to copy the data from web using by following link
    www.tcmb.gov.tr/WPS/wcm/connect/EN/TCMP+EN/Main+Menu/Statistics/Exchange+Rates/Indicative+Exchange+Rates#

    ReplyDelete

StumbleUpon
Share on Facebook
Related Posts Plugin for WordPress, Blogger...
Download Windows Live Toolbar and personalize your Web experience! Add custom buttons to get the information you care about most.