VBA to enter login and password in a website

neerakaru

New Member
Joined
Aug 31, 2010
Messages
13
Is there a way to lets say go to www.gmail.com and automatically have VBA fill in the user name and password ? I am trying to automate login for my co workers to a certain site. Just havent been able to find out how. PLZ HELP !!
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
I used to use this code to populate cells with Internet Explorer. It doesn't seem to like Excel 2010. The main thing would be to see what the buttons are called in the source of the webpage, then use Sendkeys.

Code:
Sub Search2()

Dim IE As Object
    Application.DisplayAlerts = False
    
Dim Max
Dim Name
Dim A

Range("A1").Select
Max = ActiveCell.Value

Range("b16").Select

For A = 1 To Max

    Name = ActiveCell.Value
    
    Set IE = CreateObject("InternetExplorer.Application")
    'create new instance of IE

    IE.Navigate "http://www.google.com"
    'go to web page listed inside quotes

    IE.Visible = True

    While IE.Busy
        DoEvents    'wait until IE is done loading the page
    Wend
    
'    While IE.Busy
'        DoEvents    'wait until IE is done loading the page
'    Wend

IE.Document.all("q").Value = Name  ' Email Address
'IE.Document.all("q").Value = Name  ' Password

While IE.Busy
        DoEvents    'wait until IE is done loading the page
Wend

IE.Document.all("btnG").Click

'Y = 8
'For X = 1 To Y
'  SendKeys "{TAB}", True
'  SendKeys "{ENTER}", True
'Next X
                                   'SendKeys "password", True

While IE.Busy
        DoEvents    'wait until IE is done loading the page
Wend

IE.Quit

  ActiveCell.Offset(1, 0).Activate

Next A

End Sub
[\code]
 
Upvote 0
There should be no need for SendKeys.

This should submit the login form for gmail.
Code:
Set frm = IE.Document.getelementbyid("gaia_loginform")
 
frm.Submit
 
Upvote 0
That really Didnt work .. for example I created a button so when i press it, it opens up the website and then enters the username and password...


Sub Oval6_Click()


Dim IE As Object
Application.DisplayAlerts = False

Dim Max
Dim Name
Dim A

Range("A1").Select
Max = ActiveCell.Value

Range("b16").Select

For A = 1 To Max

Name = ActiveCell.Value

Set IE = CreateObject("InternetExplorer.Application")
'create new instance of IE

IE.Navigate "https://ritx-fl-sales.bswa.net/(S(zf3kw2qv3vnodv3chisjwaq2))/Login.aspx"
'go to web page listed inside quotes

IE.Visible = True

While IE.Busy
DoEvents 'wait until IE is done loading the page
Wend

' While IE.Busy
' DoEvents 'wait until IE is done loading the page
' Wend

IE.Document.all("q").Value = AF1388559802
'IE.Document.all("q").Value = 47050737


While IE.Busy
DoEvents 'wait until IE is done loading the page
Wend

IE.Document.all("btnG").Click

'Y = 8
'For X = 1 To Y
' SendKeys "{TAB}", True
' SendKeys "{ENTER}", True
'Next X
'SendKeys "password", True

While IE.Busy
DoEvents 'wait until IE is done loading the page
Wend

IE.Quit

ActiveCell.Offset(1, 0).Activate

Next A


End Sub


It didnt do anything at all.
 
Upvote 0
Try:

This will log you into Gmail, alter as needed (See notes below first!!!)
Code:
Sub Test()

    Const cURL = "http://mail.google.com" 'Enter the web address here
    Const cUsername = "XXXXXX" 'Enter your user name here
    Const cPassword = "XXXXXX" 'Enter your Password here
    
    Dim IE As InternetExplorer
    Dim doc As HTMLDocument
    Dim LoginForm As HTMLFormElement
    Dim UserNameInputBox As HTMLInputElement
    Dim PasswordInputBox As HTMLInputElement
    Dim SignInButton As HTMLInputButtonElement
    Dim HTMLelement As IHTMLElement
    Dim qt As QueryTable
        
    Set IE = New InternetExplorer
    
    IE.Visible = True
    IE.Navigate cURL
    
    'Wait for initial page to load
    
    Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
    
    Set doc = IE.Document
    
    'Get the only form on the page
    
    Set LoginForm = doc.forms(0)
    
    'Get the User Name textbox and populate it
    'input name="Email" id="Email" size="18" value="" class="gaia le val" type="text"
 
    Set UserNameInputBox = LoginForm.elements("Email")
    UserNameInputBox.Value = cUsername
    
    'Get the password textbox and populate it
    'input name="Passwd" id="Passwd" size="18" class="gaia le val" type="password"


    Set PasswordInputBox = LoginForm.elements("Passwd")
    PasswordInputBox.Value = cPassword
    
    'Get the form input button and click it
    'input class="gaia le button" name="signIn" id="signIn" value="Sign in" type="submit"
    
    Set SignInButton = LoginForm.elements("signIn")
    SignInButton.Click
            
    'Wait for the new page to load
    
    Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
    
    End Sub
A few notes on this code:


  • This is not all of my own work it must be credited to whom ever was the creator (I cannot remember where i got the framework code from)
  • You will need to add in two references for this code--> in VB editor goto tools references and select both microsoft internet controls and microsoft HTML object library
  • To find the required forms and button for your particular website you will need to look in the page source (Use firefox rather than IE as you can view selection)
Hope this helps,

Ash.
 
Upvote 0
wow...this is great.....

is there a way to make the code enter the id and password in the login form popped up by the excel addins (say SAS FM addin)..?
 
Upvote 0
My button on firefox has this code:

<input name="search" value="SEARCH" type="submit">

How Can I make VBA click on this ?

thanks




Try:

This will log you into Gmail, alter as needed (See notes below first!!!)
Code:
Sub Test()

    Const cURL = "http://mail.google.com" 'Enter the web address here
    Const cUsername = "XXXXXX" 'Enter your user name here
    Const cPassword = "XXXXXX" 'Enter your Password here
    
    Dim IE As InternetExplorer
    Dim doc As HTMLDocument
    Dim LoginForm As HTMLFormElement
    Dim UserNameInputBox As HTMLInputElement
    Dim PasswordInputBox As HTMLInputElement
    Dim SignInButton As HTMLInputButtonElement
    Dim HTMLelement As IHTMLElement
    Dim qt As QueryTable
        
    Set IE = New InternetExplorer
    
    IE.Visible = True
    IE.Navigate cURL
    
    'Wait for initial page to load
    
    Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
    
    Set doc = IE.Document
    
    'Get the only form on the page
    
    Set LoginForm = doc.forms(0)
    
    'Get the User Name textbox and populate it
    'input name="Email" id="Email" size="18" value="" class="gaia le val" type="text"
 
    Set UserNameInputBox = LoginForm.elements("Email")
    UserNameInputBox.Value = cUsername
    
    'Get the password textbox and populate it
    'input name="Passwd" id="Passwd" size="18" class="gaia le val" type="password"


    Set PasswordInputBox = LoginForm.elements("Passwd")
    PasswordInputBox.Value = cPassword
    
    'Get the form input button and click it
    'input class="gaia le button" name="signIn" id="signIn" value="Sign in" type="submit"
    
    Set SignInButton = LoginForm.elements("signIn")
    SignInButton.Click
            
    'Wait for the new page to load
    
    Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
    
    End Sub
A few notes on this code:


  • This is not all of my own work it must be credited to whom ever was the creator (I cannot remember where i got the framework code from)
  • You will need to add in two references for this code--> in VB editor goto tools references and select both microsoft internet controls and microsoft HTML object library
  • To find the required forms and button for your particular website you will need to look in the page source (Use firefox rather than IE as you can view selection)
Hope this helps,

Ash.
 
Upvote 0

Forum statistics

Threads
1,224,606
Messages
6,179,866
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top