Using VBA to login to a website

dyanisis2

New Member
Joined
Jan 2, 2015
Messages
12
I have an account with Morningstar.com and I wrote a vba script to automatically log me in. It worked great for the last year, however, it appears that they've updated the login page and I cannot figure out how to fix my code to automatically login again. The login web page is:
https://www.morningstar.com/members/login.html?vurl=http://www.morningstar.com

A snippet of my code looks like this:

Code:
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = True
ie.Navigate "https://members.morningstar.com/memberservice/login.aspx"

Do Until ie.ReadyState = 4
 DoEvents
Loop

'Enter Username and Password in textboxes
Set UserN = ie.Document.getElementById("email-input-area")
If Not UserN Is Nothing Then
    UserN.all(0).Value = XXXXXXXXX
End If

' password
Set PW = ie.Document.getElementById("password-input-area")
If Not PW Is Nothing Then
    PW.all(0).Value = XXXXXXXX
End If

'Click the login button

'Old Method - M* updated their website
'Set ElementCol = ie.Document.getElementById("go_button")
'ElementCol.Click

'New Method
Set ElementCol = ie.Document.getElementsByTagName("input")(3)
ElementCol.Click

I obviously replaced my username and password with X's. The problem is that the webpage is interactive in the fact that it won't even let you click the "sign in" button unless you've entered an appropriate username and password. The code above will enter my info into the username/password fields, but it's like the webpage doesn't realize their is data in the fields, and it won't let me execute the sign in button. However, if i deleted the last character of the username and password and re-enter them in with the keyboard, then it'll let me click the sign in button, or execute the ElementCol.Click vba code. Does anyone know what to do to make this work? Any help would be appreciated. Thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Have you tried something like this:
Code:
'New Method
With IE.Document.Forms("form")
    .all("submit").Click
End With

Unfortunately, the company I work for requires us to use IE 8 for one of our systems, so I cannot test to see if it's working.
 
Last edited:
Upvote 0
oops... sent the wrong method.

Code:
Set ElementCol = IE.Document.getElementsByTagName("submit")
[/FONT][FONT=Verdana]ElementCol.all("submit").Click[/FONT]

Assuming you have the references loaded for:
- Microsoft Internet Controls
- Microsoft HTML Object Library


Assuming you have the references loaded for:
- Microsoft Internet Controls
- Microsoft HTML Object Library
 
Upvote 0
Sounds like page's front end validation kicks in response to keyup/keydown/keypress events on username and password fields. You can try to fire those events in your code and see which one works, e.g.

Code:
oField.fireEvent("keydown")
 
Upvote 0
Sounds like page's front end validation kicks in response to keyup/keydown/keypress events on username and password fields. You can try to fire those events in your code and see which one works, e.g.

Code:
oField.fireEvent("keydown")

That sounds like a good idea. Would I type it just as you have written, or how do I incorporate that line of code into my code?
 
Upvote 0
You need to call the fireevent method on the username and password text input fields on the webpage right after you set their values.
Something like this:


Code:
UserN.all(0).Value = XXXXXXXXX
UserN.all(0).fireEvent("keydown")


...


PW.all(0).Value = XXXXXXX
PW.all(0).fireEvent("keydown")

Note: You might need to try and figure out which event to fire by trial-and-error approach. It may be onchange, keyup, keydown, keypress, or whatever developer decided to implement.
 
Upvote 0
You need to call the fireevent method on the username and password text input fields on the webpage right after you set their values.
Something like this:


Code:
UserN.all(0).Value = XXXXXXXXX
UserN.all(0).fireEvent("keydown")


...


PW.all(0).Value = XXXXXXX
PW.all(0).fireEvent("keydown")

Note: You might need to try and figure out which event to fire by trial-and-error approach. It may be onchange, keyup, keydown, keypress, or whatever developer decided to implement.

I tried to add the FireEvent methods to my code and I get a runtime error, Invalid Argument. Not sure what the problem is.
 
Upvote 0
Well I think I got lucky and figured out how to make it work. I figured I'd post it here for informational purposes. I'm not a great computer coder and everything I've learned in using vba to automate IE was through many pages of google searches along with a lot of trial and error. This is what I got to work.
Code:
Set evt = ie.Document.createEvent("keyboardevent")
    evt.initEvent "change", True, False
    PW.all(0).dispatchEvent evt

I added those three lines of code to both the username and password lines. Apparently FireEvent doesn't work in later versions of IE? I truly don't understand exactly how the lines of code work, or how they interact with the native webpage code, but they work.
 
Upvote 0
Ah yes, the compatibility issue. FireEvent doesn't seem to work in later versions of IE. DispatchEvent is the new way to do that.

So in your code
1. CreateEvent creates an event object whenever a keyboard operation occurs on webpage ("keyboardevent").
2. Set the property of this event object to fire change event on the control ("change"), allow the event to propagate to other elements up in DOM hierarchy (true) and set it as non-cancelable (false).
3. Finally associate this event with the field on which you want the event be sent.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,958
Members
449,096
Latest member
Anshu121

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