Excel VBA to Submit UserName & Password on IE

BPtexan

New Member
Joined
Jul 4, 2009
Messages
24
I posted this on an earlier thread, but was urged to start a new one.

I'm trying to create a program that will automatically log in to a website that requires a username & password. I am able to enter the username and password in the correct text boxes, but I can not get the form to submit to approve the login. It refreshes and removes the password.

I have attached my code. The web url is inside it. Thanks!

Code:
Sub IE_login()
    Dim ie As InternetExplorer
    Dim C
    Dim ULogin As Boolean, ieForm
    Dim MyPass As String, MyLogin As String
    Set ie = New InternetExplorer
    ie.Visible = True
    ie.Navigate "[URL]https://applications.dacgroup.com/login.aspx[/URL]"
    'Loop until ie page is fully loaded
    Do Until ie.ReadyState = READYSTATE_COMPLETE
    Loop
    'Look for password Form by finding test "Password"
    For Each ieForm In ie.Document.forms
            ieForm(5).Value = "****"
            ieForm(6).Value = "****"
            'login
            ieForm.submit
            Exit For
    Next
    End Sub
 
I have tried it in firefox using: "This Frame -> View Frame Source and it gives exactly the same code as posted previously. Any other way to tackle this .jsp thing ? Thanks for your help
 
Upvote 0

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Dear John, why am i getting this error messages: "Run-time error'-2147023179 (800706b5)': Automation Error The Interface is unknown" ?
i've write mine exactly as exampled, i've changed only document form into (aspnetForm), the username inputbox ("ctl00$ContentMain$Login1$UserName"), the password inputbox ("ctl00$ContentMain$Login1$Password") and the button ("ctl00$ContentMain$Login1$LoginButton"). When i debug it highlight
Code:
Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
so i added
Code:
On Error Resume Next
before it, instead those loops are going forever T_T? need your help please....:confused:

Try this, based on the code in the other thread.

AFAIK there are 2 ways of submitting a form: calling the form's submit method; or callling the form input button's click method. In this case I think submit (i.e. LoginForm.submit) is insufficient because the input button has an o n c l i c k attribute which calls a Javascript function which does some sort of validation of the page (I haven't looked closely at the Javascript code). Therefore the button's click event must be called instead so that the Javascript function is called. I'm guessing that LoginForm.submit doesn't fire the o n c l i c k event.

Code:
'Needs references to Microsoft HTML Object Library and Microsoft Internet Controls
 
Option Explicit
 
Sub Test()
 
    Const cURL = "https://applications.dacgroup.com/login.aspx"
    Const cUsername = "XXXX"    'REPLACE XXXX WITH YOUR USER NAME
    Const cPassword = "YYYY"    'REPLACE YYYY WITH YOUR PASSWORD
 
    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
 
    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="ctl00$ct$UserName" type="text" maxlength="30" id="ctl00_ct_UserName" style="width:160px;" />
 
    Set UserNameInputBox = LoginForm.elements("ctl00$ct$UserName")
    UserNameInputBox.Value = cUsername
 
    'Get the password textbox and populate it
    '< input name="ctl00$ct$Password" type="password" maxlength="30" id="ctl00_ct_Password" style="width:160px;" />
 
    Set PasswordInputBox = LoginForm.elements("ctl00$ct$Password")
    PasswordInputBox.Value = cPassword
 
    'Get the form input button and click it
    '< input type="submit" name="ctl00$ct$uxBtnLogin" value="Sign In" o n c l i c k="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ct$uxBtnLogin", "", true, "Login", "", false, false))" id="ctl00_ct_uxBtnLogin" />
 
    Set SignInButton = LoginForm.elements("ctl00$ct$uxBtnLogin")
    SignInButton.Click
 
    'Wait for the new page to load
 
    Do While IE.readyState <> READYSTATE_COMPLETE Or IE.busy: DoEvents: Loop
 
    'Get the HTML document of the new page
 
    Set doc = IE.document
 
    'Determine whether login succeeded or not
 
    If InStr(doc.body.innerText, "Invalid Login Information") = 0 Then
        MsgBox "Login succeeded"
    Else
        MsgBox "Login failed"
    End If
 
    Debug.Print "Current URL: " & IE.LocationURL
 
End Sub
 
Upvote 0
I too have a similar issue. I have tried adapting the codes put forward above into my code, but with no luck. My experience in vba code is limited to trying to adapt others code to my own work, but this is the first i have tried retrieving information from the web.

I am trying to enter Account Number and PIN, and submit on the following website: https://www.guelphhydro.com/AccountOnlineWeb/Login.jsp. I have future plans to add further code to enter dates and download and import a .csv file into excel. If you are willing to assist any of all of this i would certainly appreciate the help.

Cheers.
 
Upvote 0
Nope, that doesn't look like HTML. Are you going to http://mtcolifants/faces/loginProper.jsp, right-clicking and viewing source? If the login form is part of a frame make sure you're looking at the frame's source, not the frameset source - Firefox is better than IE for this.

Also, with the page being .jsp this whole thing might not work.

Hi,

Please use the below code. It will get you in the wed site.

Sub IE_login()
Dim ie As InternetExplorer
Dim C
Dim ULogin As Boolean, ieForm
Dim Username As String, Password As String
Set ie = New InternetExplorer
ie.Visible = True
ie.Navigate "XXXXXXXXXXXXXX" ' Replace XXXXXXX with your URL
'Loop until ie page is fully loaded
Do Until ie.ReadyState = READYSTATE_COMPLETE
Loop
'Look for password Form by finding test "Password"
For Each ieForm In ie.Document.forms
ieForm(6).Value = "YYYYY" ' Please replace YYYYY With your Username
ieForm(7).Value = "XXXXX" ' Please replace XXXXX with your Password
'login

Set SignInButton = ieForm(9)
SignInButton.Click

Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,272
Messages
6,129,822
Members
449,538
Latest member
cookie2956

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