Select Radio Button on webpage error message using Excel VBA

Moeey

New Member
Joined
Sep 13, 2014
Messages
34
Hello,
I am getting the following error message
Run-time error '91': Object variable or With block variable not set.

Here's my code, and the line highlighted in red is where the error message occurs
Code:
Public Sub Login()
    
    Dim IE As InternetExplorer
    Dim HTMLdoc As HTMLDocument
    Dim button As HTMLInputButtonElement
    Dim i As Integer
    
    Set IE = New InternetExplorer
    With IE
        .Visible = True
        .navigate "URL"
        While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend
        Set HTMLdoc = .document
    End With
    
    Set UserName = HTMLdoc.getElementById("ctl00$ContentBody$LoginControl1$txtUserName")
    Set Password = HTMLdoc.getElementById("ctl00_ContentBody_LoginControl1_txtPassword")
    UserName.Value = "Username"
    Password.Value = "Password"
    Set SignIn = HTMLdoc.getElementById("ctl00_ContentBody_LoginControl1_butLogon")
    SignIn.Click
    
'Choose Site
    While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
        
    Set button = Nothing
    i = 0
    While i < HTMLdoc.Links.Length And button Is Nothing
        If InStr(HTMLdoc.Links(i).href, "ctl00$ContentBody$rptSites$ctl00$ctl00") > 0 Then Set button = HTMLdoc.Links(i)
        i = i + 1
    Wend
    
    If Not button Is Nothing Then
       button.Click
    Else
        MsgBox "Button not found"
    End If
    
'Choose what you want to do in SoftSmart
    While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
    
    Set button = HTMLdoc.getElementById("linkMailMerge")
    button.Click
    
'Choose detailed data extract
    While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
        
[COLOR=#ff0000][B]  HTMLdoc.getElementById("rdoDetailedExtract").Checked = True
[/B][/COLOR]    
End Sub
Here's the html behind the radio button which I am trying to select.
HTML:
any ideas where I'm going wrong?

Thanks
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Sorry here's the HTML behind the radio button
HTML:
input name="" id="rdoDetailedExtract" type="radio" value="rdoDetailedExtract"/
 
Upvote 0
The error means that the element with id "rdoDetailedExtract" doesn't exist. Immediately before the red line put:
Code:
Set HTMLdoc = IE.document
to load the HTMLDocument from the current IE page.
 
Upvote 0
Thanks John, unfortunately I'm on my hols at the mo, and I'm not set up to access the intranet from my own laptop so I can't test your suggestion.
But will try it out as soon as I'm back and let you know how I get on.
 
Upvote 0
Hello John,
Have amended my code with your suggestion but still getting the error message,
Run-time error '91': Object variable or With block variable not set.

Not sure if this has anything to do with it, but the radio button I am trying to select is grouped with two other radio buttons.
When the page loads one of the other radio button is selected by default.

Here is the HTML of the radio button which is selected by default when the page loads,
HTML:
input name="" id="rdoCorrespondee" type="radio" CHECKED="checked" cachedHoverStateItem="[object Object]" value="rdoCorrespondee"/
 
Upvote 0
Maybe it is a timing issue with the radio button with id = "rdoDetailedExtract" not being available when the line

HTMLdoc.getElementById("rdoDetailedExtract").Checked = True

is executed. You can test this by running the code in your first post. Let the error message display, click Debug and drag the yellow execution pointer back to the above line and press the F8 key to execute the line again. If that works then it confirms a timing issue and you can use the following code to wait for the radio button to become available before setting its Checked property.

Code:
    Dim rb As HTMLInputElement
    Do
        Set rb = HTMLdoc.getElementById("rdoDetailedExtract")
        DoEvents
    Loop While rb Is Nothing
    rb.Checked = True
 
Upvote 0
Hello John,
Once again thanks for taking the time to answer my query. Feels like I have a personal tutor. Definitely owe you a drink.

So I did what you suggested. Ran the code, and when it crashed with the error message, put in into debug and dragged the yellow pointer back to line mentioned. Unfortunately I was still getting the same error message.

So then I decided to put your code in anyway.

HTML:
While IE.Busy Or IE.readyState <> READYSTATE_COMPLETE: DoEvents: Wend
    Set HTMLdoc = IE.document
    
Dim rb As HTMLInputElement
    
    Do
        Set rb = HTMLdoc.getElementById("rdoDetailedExtract")
        DoEvents
    Loop While rb Is Nothing
    rb.Checked = True
    
    HTMLdoc.getElementById("rdoDetailedExtract").Checked = True

I ran it to see the value of rb.
During the debug the code never exits the Do loop and the variable rb always stays as Nothing.
 
Upvote 0
Is the radio button in a frame or iframe? If it is then your HTMLdoc object may not be referring to the document in which the radio button resides. You can determine this by right-clicking the web page near the radio button in your browser and seeing if the context menu which pops up has a 'View frame' or similar option (this works in Firefox, I'm not sure about IE).
 
Upvote 0

Forum statistics

Threads
1,223,635
Messages
6,173,479
Members
452,516
Latest member
archcalx

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