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-comfficeffice" /><o></o>
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></o>
Please review my code to see if I am missing something else to logon to:<o></o>
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> </o>
Option Explicit
<o> </o>
Public Enum IE_READYSTATE
Uninitialised = 0
Loading = 1
Loaded = 2
Interactive = 3
complete = 4
End Enum
<o> </o>
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> </o>
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> </o>
'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> </o>
On Error GoTo 0
Exit Sub
<o> </o>
Test_Error:
<o> </o>
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Test of VBA Document Sheet4"
End Sub
<o> </o>
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></o>
Please review my code to see if I am missing something else to logon to:<o></o>
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> </o>
Option Explicit
<o> </o>
Public Enum IE_READYSTATE
Uninitialised = 0
Loading = 1
Loaded = 2
Interactive = 3
complete = 4
End Enum
<o> </o>
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> </o>
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> </o>
'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> </o>
On Error GoTo 0
Exit Sub
<o> </o>
Test_Error:
<o> </o>
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Test of VBA Document Sheet4"
End Sub
<o> </o>