Avatar of srikanthv2322
srikanthv2322
 asked on

VBA Excel to Log into Google

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
Microsoft ExcelVB Script

Avatar of undefined
Last Comment
SiddharthRout

8/22/2022 - Mon
SiddharthRout

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
srikanthv2322

ASKER
How can we access an link in a webpage through VBA
SiddharthRout

lemme check. :)

However did you check if the above works?

Sid
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SiddharthRout

When you type Google.com from your country which link does it go to?

Sid
srikanthv2322

ASKER
Its navigating to -http://www.google.com/
I am in USA
SiddharthRout

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

Sid
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
srikanthv2322

ASKER
Yeah i tried it , its working fine
SiddharthRout

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
SiddharthRout

Once you confirm that the above is working, I will add the error handling.

Sid
Your help has saved me hundreds of hours of internet surfing.
fblack61
srikanthv2322

ASKER
No Its not working...
Error-'Method 'Document' of object 'IWebBrowser2' failed'
SiddharthRout

IWebBrowser2?

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

Sid
srikanthv2322

ASKER
No i didnt make any changes to the code,Yes i am sure that i didnt login to Google
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SiddharthRout

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
srikanthv2322

ASKER
rumtime error '438- object doesnt support this property or method
SiddharthRout

Are you doing this from Excel?

Sid
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
srikanthv2322

ASKER
YES I AM DOING FROM EXCEL, AT A CLICK OF COMMAND BUTTON
ASKER CERTIFIED SOLUTION
SiddharthRout

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
srikanthv2322

ASKER
Its not working out for me, i am using Windows 7, i am having 3 browsers on my systeem-Mozilla, IE7,Chrome
SiddharthRout

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

Sid
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
srikanthv2322

ASKER
How can i call chrome browser, lets try with this instead of IE 7
SiddharthRout

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
SiddharthRout

Try upgrading to IE 8.

Sid
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23