PDA

View Full Version : VBA code to click submit button in ie after checkbox is checked



b.hill
04-24-2012, 07:54 PM
The code below works for selecting the checkbox when I am already logged in (in this scenario the log in screen never shows up when a new ie is created), but when I am not logged in the code works for logging me in, but then will not select the checkbox once logged in. In both scenarios I am having trouble clicking submit after selecting the checkbox. Can anyone help me fix either of these two issues? Thank you in advance.
Note: The submit button only becomes available to click after a checkbox is selected.



Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()

Dim oHTML_Element As IHTMLElement
Dim sURL As String

On Error GoTo Err_Clear
sURL = "**************"
Set oBrowser = New InternetExplorer
oBrowser.Silent = True
oBrowser.timeout = 60
oBrowser.navigate sURL
oBrowser.Visible = True

Do
Loop Until oBrowser.readyState = READYSTATE_COMPLETE

Set HTMLDoc = oBrowser.document

HTMLDoc.all.loginEmail.Value = "*********"
HTMLDoc.all.loginPassword.Value = "******"

For Each oHTML_Element In HTMLDoc.getElementsByTagName("button")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next

Err_Clear:
If Err <> 0 Then
Err.Clear
Resume Next
End If

HTMLDoc.all("selected_10").Click

For Each oHTML_Element In HTMLDoc.getElementsByTagName("button")
If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
Next

If Err <> 0 Then
Err.Clear
Resume Next
End If

I've copied the source code below for the checkbox and the submit button.

Checkbox:
<td class="select"><input id="selected_10" name="selected_10" type="checkbox"></td>

Submit button:
<div class="buttonBar">
<button class="btn-continue orange" name="continue" type="submit" value="continue">Continue</button>

Crocus Crow
04-25-2012, 04:29 AM
Why are you using On Error? That could be hiding the underlying problem, so get rid of all the error handling statements.

The problem could be a timing issue (e.g. document not complete at the time the code accesses it), so try stepping through the code with F8 in the VB editor.

If still no joy, I will need the URL to help you further.