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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code for Excel VBA to interact with web page - click on radio button?

Status
Not open for further replies.

chemebabak

Technical User
Mar 28, 2012
12
US
So I want to use Excel VBA to open and manipulate websites. I have selected Microsoft HTML Object Library and Microsoft Internet Controls in VBA references. I want to select a radio button from a group using excel VBA. I keep getting a Run-time error '13': Type mismatch. on the radio button variable:

Code:
'    Dim TempUnits As HTMLButtonElement
'    Dim TempUnits As HTMLElementCollection
'    Dim TempUnits As HTMLInputButtonElement
'    Dim TempUnits As HTMLInputElement
'    Dim TempUnits As HTMLOption
'    Dim TempUnits As HTMLOptionButtonElement
    Dim TempUnits As HTMLOptionElement
    Set TempUnits = webbookForm("TUnit")
    TempUnits.Value = "F"
I have tried dimming the different MSHTML classes for radio button, including "Dim TempUnits As Object". They all give a type mismatch. What am I doing wrong?

Code:
Sub NIST_webbook()

'
    Const cURL = "[URL unfurl="true"]http://webbook.nist.gov/chemistry/fluid/"[/URL] 'Enter the web address here'
    Dim IE As InternetExplorer
    Set IE = New InternetExplorer
    IE.Visible = True
    IE.Navigate cURL
    'Wait for initial page to load
    Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
    
    Dim doc As HTMLDocument
    Set doc = IE.Document
    
    'Get the only form on the page
    Dim webbookForm As HTMLFormElement
    Set webbookForm = doc.forms(0)
    
    Dim ChemSpeciesDropdownBox As HTMLDListElement
    Set ChemSpeciesDropdownBox = webbookForm("ID")
    ChemSpeciesDropdownBox.Value = "C106978"
    
    Dim TempUnits As HTMLButtonElement
    Set TempUnits = webbookForm("TUnit")
    TempUnits.Value = "F"
    
    
'    NextButton.Click
'    '
'    '
'    'Wait for the new page to load
'
'    Do While IE.ReadyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop
    
    End Sub
 
Did you try to Dim it as Variant ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
PHV, I just tried that and it does not work. For a variant, the error is Run-time error '438': Object does not support this property or method.
 
chemebabak,
[tt]TempUnits[/tt] is a [tt]DispHTMLElementCollection[/tt] of [tt]HTMLInputElement[/tt]'s

You will need to loop through the Collection...
[tt] Dim item
...
For Each item In TempUnits
'TempUnits.Value = "F"
If item.Value = "F" Then item.Checked = True
Next item
...[/tt]

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
Ok, so is:

Dim TempUnits as DispHTMLElementCollection
or
Dim TempUnits as HTMLInputElement

And

Dim item as DispHTMLElementCollection
or
Dim item as HTMLInputElement
?
 
[tt]Dim TempUnits as DispHTMLElementCollection
Dim item as HTMLInputElement[/tt]

If you get an error on [tt]item[/tt] change in back to a variant/object and use [tt]TypeName()[/tt]or the Object Browser to see what type of object it is.

Hope this helps,
CMP

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
CMP,

Code:
Dim TempUnits As DispHTMLElementCollection
MsgBox "TempUnits is " & TypeName(TempUnits)
MsgBox "webbookForm('TUnit') is " & TypeName(webbookForm("TUnit"))
Set TempUnits = webbookForm("TUnit")

TypeName shows TempUnits as "Nothing" and webbookForm("TUnit") as "DispHTMLElementCollection"

Thank you for your continued assistance.

 
So I tried

Code:
Dim TempUnits As Variant
and
Code:
Dim TempUnits As Object

Both worked! Thanks!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top