Secure web site logon

richardlh

New Member
Joined
Jul 30, 2010
Messages
21
I followed the guidance given in an earlier thread and cannot get my vba code to enter the user ID and password. The code pulls up the web site and puts the cursor in the user ID box. But it stops there and renders the error code 91, "Object variable not defined".<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
I pulled up the web site in IE8 and pressed F12 as suggested in the previous thread and was able to find the form that holds the user ID and password boxes. The form name is "Logon" so I substituted it for "0" [Set PageForm = doc.forms("Logon" vs Set PageForm = doc.forms(0)]. Is this valid.<o:p></o:p>
Please review my code to see if I am missing something else to logon to:<o:p></o:p>
https://www.usaa.com/inet/ent_logon/Logon
Here’s the code (Note code includes line numbers):
'Need reference to Microsoft HTML Object Library. Select this in Tools - References in VB editor.
<o:p> </o:p>
Option Explicit
<o:p> </o:p>
Public Enum IE_READYSTATE
Uninitialised = 0
Loading = 1
Loaded = 2
Interactive = 3
complete = 4
End Enum
<o:p> </o:p>
Sub Test()

Const cURL = "https://www.usaa.com/inet/ent_logon/Logon"
Const cUserID = "XXXX" 'REPLACE XXXX WITH YOUR USER ID
Const cPwd = "YYYY" 'REPLACE YYYY WITH YOUR PASSWORD

Dim ie As Object
Dim doc As HTMLDocument
Dim PageForm As HTMLFormElement
Dim UserIdBox As HTMLInputElement
Dim PasswordBox As HTMLInputElement
Dim FormButton As HTMLInputButtonElement
Dim Elem As IHTMLElement

On Error GoTo Test_Error
<o:p> </o:p>
10 Set ie = CreateObject("InternetExplorer.Application")

20 ie.Visible = True
30 ie.navigate cURL

'Wait for initial page to load

40 Do While ie.busy Or Not ie.readyState = IE_READYSTATE.complete: DoEvents: Loop

50 Set doc = ie.document

'Output HTML tags to debug window

60 Debug.Print "Login page: " & ie.LocationURL
70 For Each Elem In doc.all
'Debug.Print Elem.tagName
80 Next
<o:p> </o:p>
'Get the only form on the page

90 Set PageForm = doc.forms("Logon")

'Get the User Id textbox
'< input class="TextBox" maxlength="15" name="UserName" size="12">

100 Set UserIdBox = PageForm.elements("j_username")

'Set the User Id

110 UserIdBox.Value = cUserID

'Get the password textbox
'< input class="TextBox" type="password" maxlength="10" name="Password" size="12">

120 Set PasswordBox = PageForm.elements("j_password")

'Set the password

130 PasswordBox.Value = cPwd

'Submit the form (like clicking the 'Submit' button) to navigate to next page

140 PageForm.submit

'Wait for the new page to load

150 Do While ie.busy Or Not ie.readyState = IE_READYSTATE.complete: DoEvents: Loop

'Get the HTML document of the new page

160 Set doc = ie.document

'Output HTML tags to debug window to prove this is the new page

170 Debug.Print "Terms of Use page: " & ie.LocationURL
180 For Each Elem In doc.all
'Debug.Print Elem.tagName
190 Next

'The new page contains 'Terms of Use' conditions and an 'Accept' button within a form
'Get the only form on the page

<o:p> </o:p>
On Error GoTo 0
Exit Sub
<o:p> </o:p>
Test_Error:
<o:p> </o:p>
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Test of VBA Document Sheet4"

End Sub
<o:p> </o:p>
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
What previous thread?

Does it work if you don't change the 0?

Sometimes it's easier to refer to members of a collection, eg forms, by their index.

There are also other ways to get a reference to elements on a web page.

For example GetElementByID.

I'm going to take a look at the code, it is a valid URL isn't it?

One thing you might want to remove is any On Error stuff.

That could just be hiding errors.

If you remove them you'll get information, well an error message anway, about the problem.

Right just tried it and had no problems.

The only things I changed were removing the On Error stuff and hard-coding bogus ID and password.
 
Upvote 0
Thanks Norie for your quick reply! The previous thread was started on May 13th, 2008, 02:43 PM by WXUE.

Now the code works if I change it from "Logon" to 0. Why would it work now and not before? Go figure!

It is a valid URL.

Just a note to answer your question. I added the error code to try to get it to ID the line of code where the error occurs, but not there yet. I get the same error message whether I use the error trapping code or not.

Now the second part. I can login to the first page and the info is submitted, which takes me to a second page where I need to enter a PIN. Is there a part of the coding I copy and paste to a second sub or function that I can call to enter the pin and submit the second page? Thanks!
 
Upvote 0
A follow up...after I sent the last reply, I find that the coding only works if there is an instance of IE open. It does not work if IE is closed. Secondly, when I insert a break point and then check the code at

Set doc = ie.document

I see

doc = "[object]"

Is this correct? I would expect to see the value of a valid defined variable.
 
Upvote 0
Dealing with webpages/internet/URL etc can throw up all sorts of weird things.

I honestly couldn't tell you why changing the form part would make a difference.

As for the error trapping - it might actually be hiding errors rather than helping you find them, though admittedly that's more likely with On Error Resume Next.

Nothing wrong with trying to trap errors but you should probably leave it to VBA.

You'll get exactly the same information as you are getting with your message box and you'll have the advantage to Debug or even give Help a shot.

Debug would be the most useful as it should direct you to the offending line of code, Help perhaps not so much but that's Help in general.:eek:

If you are navigating to another page when you submit the form then just 'reset' doc to refer to the new document, just like you used it for the initial page.

You could create a separate variable for the new page but I don't really think its essential in this case.

I don't quite see what difference having an existing instance of IE open would make.

The code creates and uses it's own instance using CreateObject and as is doesn't go near any other instance.

This could be something to do with the quirks you can come across when doing this sort of thing, which isn't really a precise science.

People have all sorts of setups - different browser versions, connections etc

I'm not sure about your question regarding the variable doc.

What exactly did you expect?

IE.document represents an object, specifically the document object of the current page in your instance of IE.

Set doc = IE.document creates a reference to that object which you can then use in subsequent code.
 
Upvote 0
I included "Erl" in my error statement and it pointed to line #110 as the first culprit. When I set a breakpoint so I can see the values assigned to variables, I get the following information at lines:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
100 Set UserIdBox = PageForm.elements("j_username")<o:p></o:p>
<o:p></o:p>
UserIdBox = Nothing
PageForm.elements("j_username") = "Permission Denied"
<o:p></o:p>

<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
110 UserIdBox.Value = cUserID<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
UserIdBox.Value = "Object variable or With block variable not set"
cUserID = XXXX<o:p></o:p>
<o:p></o:p>
Is this why my userID and password are not being entered into the appropriate boxes on the web page when it first opens? If so, what can I do to allow permission? Thanks!
 
Last edited:
Upvote 0
Sounds to me as though the input element you are trying to find isn't being found.

That could be for a no of reasons.

One thing you might want to try is to locate it using its ID ("usaaNum") via GetElementByID.

You could also use that for the form ("Logon") and password ("usaaPass").

I don't think I've ever really tried using the elements collection and I'm not even 100% sure if it applies, or if it does how, to a form element.

You might need to refer to the form's document property.

I'll check that out and might give the code a go.

First I'll try it as is, then perhaps try my suggestion.

Not saying anything will work but it's worth a shot.:)

PS Is there a reason you aren't using the built-in error handling? What you have doesn't seem to give much more information as far as I can see.
 
Upvote 0
After reviewing the subject URL and using F12 under IE8, I wonder if I should use the POST Method? If so, how would that coding look in Excel vba for this web site? Thanks.
 
Upvote 0
I've never used that method I'm afraid, but I'm sure I've seen code here that does.

You should be able to find it by searching.

I might look into the method but this is a secure site, belonging to USAA.

So I'm a little wary about using code to access it, there could be perfectly legitimate reasons why it isn't permitted anyway.:)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,532
Messages
6,114,176
Members
448,554
Latest member
Gleisner2

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