xl to web form same name issue

tx12345

Board Regular
Joined
Aug 28, 2006
Messages
165
hi,

here is the source code from the web page:

Code:
<html>
<body>
<form name="Form" method="get" action="abc.htm" id="Form">
<table id="radio" border="0">
<tr><td>
<input id="r1" type="radio" name="asd" value="N" checked="checked"/><label for="r1">R1</label>
<td>
<input id="r2" type="radio" name="asd" value="Y"  /><label for="r2">R2</label>

</tr>
</table>
</form>
</body>
</html>

two innocent radio butonswhere by default r1 is selected and r2 is not. to turn that around, the code needs to check r2 (1) and uncheck r1 (0).

Code:
Sub test3()
Dim strtest
Set ie = CreateObject("InternetExplorer.Application")
With ie
    .Visible = True
    .navigate "http://equitytoincome.com/radio.htm"
     Do Until .readyState = 4
     DoEvents
     Loop

    Set f1 = .document.all.Item("asd")
          f1.Checked = 0
    Set f2 = .document.all.Item("asd")
          f2.Checked = 1
     
End With
End Sub

the code as written bugs out at "f1.Checked = 0" because both inputs have the same name. i have tinkered with this with 1000 variations hacking away, but can't find the tweak to account for the same name business.

any thoughts are welcome.

TIA!

tx
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Upvote 0
i revised the code to show the link

By the way why do you need 2 references to the same object?

ah! tis the dilemma. the web page has coded two radio buttons with the exact same name. by default the first one is checked and the second one is not checked, but i want to have the flexibility to check and uncheck either of them as needed.

thanks for the link.

tx
 
Upvote 0
FYI. Because there are no strict rules enforced in the naming conventions of HTML elements, the DOM provides a variety of ways to grab a reference to any element. Below is just one. You would really help yourself along if you would use early binding with this large model. This way, you would have have intellisense available too you. When you are using ie.Document you are actually late binding to an "HTMLDocument" object created via an implicit reference in the Microsoft HTML Object Library which contains the Document Object Model (DOM).


You do not need to do anything to f1 if it is grouped with f2. Simply selecting f2 will unselect f1 if it is selected...

Code:
        'Set f1 = .document.getElementById("r1")
        'f1.Checked = False
        Set f2 = .document.getElementById("r2")
        f2.Checked = True

If you were to early bind, your code would look like this:

Code:
'set references to the following two libraries
'Microsoft Internet Controls or Miscrosoft Browser Helpers
'Microsoft HTML Object Library
Sub test3()
    Dim ie As InternetExplorer
    Dim f2 As HTMLInputElement
    Dim HtmlDoc As HTMLDocument
    
    Set ie = New InternetExplorer
    
    With ie
        .Visible = True
        .navigate "http://equitytoincome.com/radio.htm"
         Do Until .readyState = READYSTATE_COMPLETE
         DoEvents
         Loop
    
        Set HtmlDoc = .document
        Set f2 = HtmlDoc.getElementById("r2")
        f2.Checked = True
          
    End With
    
End Sub
 
Upvote 0
tx

I'll maybe take a look later but I'm still a little unclear what you are trying to do here.

If you search this forum you'll find plenty of examples of how to manipulate websites/pages using VBA.

I will though say again that I'm a little bit uncomfortable with helping people with code to access secure sites.

You mentioned in your PM that your site was hacked?

How do you think whoever did that did it?

It certainly wasn't me, as I've no interest in that sort of thing.
 
Upvote 0
nice bit of code. thanks much. that ".getElementBy" statement will come in handy i am sure. all the other comments were excellent as well.
thanks again

tx
 
Upvote 0

Forum statistics

Threads
1,222,181
Messages
6,164,426
Members
451,894
Latest member
480BOY

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