Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vbscript - How to fill out IE form? 1

Status
Not open for further replies.

hayesb2

Programmer
Jul 22, 2002
19
US
Can anyone provide a sample vbscript which would be used to popoulate a text box in IE and select an option from a drop-down box?
(Reason being is I need to fill out lots of forms and do not know how to code a .vbs file to make IE active and populate...A simple example should point me in the right direction though)
Thanks.
 
Just a couple questions since I'm new to vbscript...
1) Where is the call to the funciton? Is it initiated when the
SelectRadioByValue(oIE.document.All, "Mr") = True
condition is met?

2)I will have multiple seperate Radio selections to populate. I do not see in the code where it specifically references the name of the Radio selection, so how would I do 2or3?

Your help is greatly appreciated!
 
1) Yes that is correct, if it returns true, it was able to select the radio with that value, if not, it returns false.

2) You have to go by the value of the radio instead of its name since the name is shared between the multiple radio buttons that are for the same option. We could create a function to refer to it by name and index.
Lets say we have:
Code:
<input type="RADIO" name="prefix" value="Mr" >Mr.
<input type="RADIO" name="prefix" value="Mrs" >Mrs.
<input type="RADIO" name="prefix" value="Miss" >Miss 
<input type="RADIO" name="prefix" value="Ms" >Ms

You see that we cannot tell the code to click the radio named "prefix" because there are 4 of them with that name. So, we could tell it to click the radio with the value of "Mr", "Mrs", "Miss", or "Ms".

However we could tell the code to click the 3rd radio button with the name "prefix", or just the Nth radio button on the form regardless of name (as it appears in source) by making another function like this:

Code:
Function SelectRadioByIndex(oDocAll, iRadioIndex, sRadioName)
    Dim iCnt
    Dim sValue
    Dim sType

    With oDocAll
        On Error Resume Next
        For i = 0 To (.Length - 1)
            sValue = Empty
            sType = Empty
            sValue = .Item(i).Value
            sType = .Item(i).Type

            If Not IsEmpty(sValue) And Not IsEmpty(sType) Then
                If sType = "radio" Then
                    
                    If Not IsEmpty(sRadioName) Then
                        If LCase(sRadioName) = LCase(.Item(i).Name) Then
                            iCnt = iCnt + 1
                            If iRadioIndex = iCnt Then
                                On Error GoTo 0
                                .Item(i).Click
                                SelectRadioByIndex = True
                                Exit Function
                            End If
                        End If
                    Else
                        iCnt = iCnt + 1
                        If iRadioIndex = iCnt Then
                            On Error GoTo 0
                            .Item(i).Click
                            SelectRadioByIndex = True
                            Exit Function
                        End If
                    End If
                End If
            End If
        Next
        Stop
        On Error GoTo 0
    End With
    SelectRadioByIndex = False
End Function

You can then use this to click the radio button:
Code:
    If SelectRadioByIndex(oIE.Document.All, 3, "prefix") = True Then MsgBox "SelectRadioByIndex successfully clicked 3rd 'prefix' radio button"

    If SelectRadioByIndex(oIE.Document.All, 2, empty) = True Then MsgBox "SelectRadioByIndex successfully clicked the 2nd radio button on page"

The first example clicks the Nth radio button named "prefix", the second example clicks the Nth radio button on the whole page.

Having both the first function and this new function available pretty much gives you all the flexibility you're going to get for clicking a radio button programatically.
 
Hello all,

For radio button, one can use .checked=true to set the checked which makes the others in the group unchecked. To simulate click event for checking, we can use .click also.

This is an illustration. (Early html documentation said at least one radio button is checked. If not set, the first the chosen as checked by default. This is nolonger true.)
Code:
<html>
<head>
<script language="vbscript">
sub Clear
	set ccontrol=document.forms(0).radioname
	for each ordbtn in ccontrol
		ordbtn.checked=false
	next
	set ccontrol=nothing
end sub
sub docheck
	set ccontrol=document.forms(0).radioname
	bfound=false
	for each ordbtn in ccontrol
		if ordbtn.checked=true then
			msgbox ordbtn.value & " is chosen."
			bfound=true
		end if
	next
	if bfound=false then
		msgbox "None chosen."
	end if
	set ccontrol=nothing
end sub
sub doset
	set ccontrol=document.forms(0).radioname
	for each ordbtn in ccontrol
		if ordbtn.value="Mrs" then
			ordbtn.checked=true
			exit for
		end if
	next
	set ccontrol=nothing
end sub
sub doclearall
	set ccontrol=document.forms(0).radioname
	for each ordbtn in ccontrol
		ordbtn.checked=false
	next
	set ccontrol=nothing
end sub
</script>
</head>
<body>
<form name=radioform>
<input type=radio name=radioname value="Mr" onDblClick="Clear">Mr
<input type=radio name=radioname value="Mrs" onDblClick="Clear">Mrs
<input type=radio name=radioname value="Miss" onDblClick="Clear">Miss
<input type=radio name=radioname value="Ms" onDblClick="Clear">Ms
<br><br>
<input type=button name=checkradio value="check radio" onClick="docheck">
<br><br>
<input type=button name=setradio value="set Mrs radio" onClick="doset">
<br><br>
<input type=button name=clearallradio value="clear all radio buttons" onClick="doclearall">
</form>
</body>
</html>
regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top