Solved

VBA Excel to Log into Google

Posted on 2011-03-08
22
597 Views
Last Modified: 2012-05-11
I want to open google site and click on 'Sign in' link at the top right, i want to enter Google login credentials and even checking 'Stay signed in' check box. all at a click of Command Button in Excel
I will provide the User ID and Password and Yes/No value for 'Stay signed in' checkbox in the excel spreadsheet cells itself

Want to know how to identify elements in a webpage through VBA to access
0
Comment
Question by:srikanthv2322
  • 13
  • 9
22 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35078168
It is very simple :)

First open a web browser and manually click the SignIn link on top. It will take you to the Sign In page. Copy the URL and paste the URL in the code below and run it :)

Sub LogOn()
    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")

    ie.Visible = True
    
    '~~> Paste URL here
    ie.Navigate "https://www.google.com/accounts/Login?hl=en&continue=http://www.google.co.in/%23q%3Dvba%2Bcode%2Bto%2Bsign%2Bon%2Bto%2Ba%2Bwebsite%26hl%3Den%26biw%3D1280%26bih%3D619"
    
    '~~> Wait until page is loaded.
    While ie.ReadyState < 4
        DoEvents
    Wend
           
    ie.Document.All("Email").Value = "USERNAME"
    ie.Document.All("Passwd").Value = "PASSWORD"
    ie.Document.All("signIn").Click
       
    '~~> Wait until page is loaded.
    While ie.ReadyState < 4
        DoEvents
    Wend
End Sub

Open in new window


Sid
0
 

Author Comment

by:srikanthv2322
ID: 35078299
How can we access an link in a webpage through VBA
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35078310
lemme check. :)

However did you check if the above works?

Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35078347
When you type Google.com from your country which link does it go to?

Sid
0
 

Author Comment

by:srikanthv2322
ID: 35078396
Its navigating to -http://www.google.com/
I am in USA
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35078409
Ok. Did you try the above code? In the meantime let me see if I can grab that link.

Sid
0
 

Author Comment

by:srikanthv2322
ID: 35078427
Yeah i tried it , its working fine
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35078437
Ok Try this. :)

Just enter the username and password int he code below.

Sub LogOn()
    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")

    ie.Visible = True
    
    '~~> Paste URL here
    ie.Navigate "google.com"
    
    For Each link In ie.Document.Links
        If link.innerText = "Sign in" Then
            link.Focus
            link.Click
        End If
    Next
    
    '~~> Wait until page is loaded.
    While ie.ReadyState < 4
        DoEvents
    Wend
           
    ie.Document.All("Email").Value = "USERNAME"
    ie.Document.All("Passwd").Value = "PASSWORD"
    ie.Document.All("signIn").Click
       
    '~~> Wait until page is loaded.
    While ie.ReadyState < 4
        DoEvents
    Wend
End Sub

Open in new window


Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35078647
Once you confirm that the above is working, I will add the error handling.

Sid
0
 

Author Comment

by:srikanthv2322
ID: 35078673
No Its not working...
Error-'Method 'Document' of object 'IWebBrowser2' failed'
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35078690
IWebBrowser2?

Did you make any changes to the code? Are you sue you are not already logged in to Google?

Sid
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:srikanthv2322
ID: 35078788
No i didnt make any changes to the code,Yes i am sure that i didnt login to Google
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35078842
I tested this and it is working. Could you check this for me. Please Change the Username and Password only.

Sub LogOn()
    Dim ie As Object

    Set ie = CreateObject("InternetExplorer.Application")

    ie.Visible = True
    
    ie.Navigate "google.com"
    
    '~~> Wait until page is loaded.
    While ie.ReadyState < 4
        DoEvents
    Wend
    
    For Each link In ie.Document.Links
        Debug.Print link.innerText
        If link.innerText = "Sign in" Then
            link.Focus
            link.Click
            Exit For
        ElseIf link.innerText = "Sign out" Then
            MsgBox "You are already signed in."
            Exit Sub
        End If
    Next
    
    '~~> Wait until page is loaded.
    While ie.ReadyState < 4
        DoEvents
    Wend
       
    On Error GoTo Sidz
    
    Application.Wait Now + TimeValue("00:00:03")
    
    ie.Document.All("Email").Value = "USERNAME"
    ie.Document.All("Passwd").Value = "PASSWORD"
    ie.Document.All("signIn").Click
       
    '~~> Wait until page is loaded.
    While ie.ReadyState < 4
        DoEvents
    Wend
    Exit Sub
Sidz:
    MsgBox Err.Description
End Sub

Open in new window


Sid
0
 

Author Comment

by:srikanthv2322
ID: 35078943
rumtime error '438- object doesnt support this property or method
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35078946
Are you doing this from Excel?

Sid
0
 

Author Comment

by:srikanthv2322
ID: 35078987
YES I AM DOING FROM EXCEL, AT A CLICK OF COMMAND BUTTON
0
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
ID: 35079068
See the snapshot. It is working for me. I am attaching the file so that you can test it without even touching the code.

Simply Enter the USERNAME and PASSWORD in cell B1 and B2

Sid
Untitled.jpg
LogOn.xls
0
 

Author Comment

by:srikanthv2322
ID: 35079133
Its not working out for me, i am using Windows 7, i am having 3 browsers on my systeem-Mozilla, IE7,Chrome
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35079147
Ok. Let me test it in Win 7. I will have to reboot. Will get back to you in 15 mins

Sid
0
 

Author Comment

by:srikanthv2322
ID: 35079178
How can i call chrome browser, lets try with this instead of IE 7
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35079208
Ok. I tested it in Win 7 / IE 8 /  Excel 2010 and it is working fine.

Do you know how to debug the code?

Sid
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35079356
Try upgrading to IE 8.

Sid
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now