HTML Object Internet Explorer automation

UnknownQ

New Member
Joined
Mar 31, 2020
Messages
5
Office Version
  1. 365
Platform
  1. Windows
How do I select an option from a dropdown menu and update the webpage.
Code:

VBA Code:
Dim IE As New SHDocVw.InternetExplorer
Dim HTMLDoc As MSHTML.IHTMLDocument

then I navigate to a URL and I fetch the dropdown menu using html id and put value=my desired value

VBA Code:
HTMLDoc.getElementById("xyz").Value = "dropdown_option"

I successfully get the option. But, the website doesn't update? e.g. filling up other fields.
I believe some javascript needs to be run. How can i fix this using VBA?

EDIT:
Also posted here VBA HTML Object Internet Explorer automation
 
Last edited by a moderator:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Welcome to MrExcel forums.

Is "dropdown_option" correct? This should not be the option's visible text but the option's value attribute, which can be different.

Another way is to set the selectedIndex:
VBA Code:
HTMLDoc.getElementById("xyz").selectedIndex = 0 '1st option

If that doesn't work use the browser tools to see if any events are defined on the dropdown (select element). Typically they have a "change" event which can be triggered with either selectElement.fireEvent "onchange", or with dispatchEvent, maybe like this:
VBA Code:
    Dim SelectElement As HTMLSelectElement
    
    #If VBA7 Then
        Dim changeEvent As DOMEvent
    #Else
        Dim changeEvent As Object
    #End If
    
    Set changeEvent = HTMLDoc.createEvent("HTMLEvents")
    changeEvent.initEvent "change", True, False

    SelectElement = HTMLDoc.getElementById("xyz")
    SelectElement.Value = "dropdown_option"
    SelectElement.dispatchEvent changeEvent
Make sure the Set changeEvent line is executed after the page has completely loaded.
 
Upvote 0
Sourcode for dropdown:
<div class="rcbSlide" style="z-index: 6000; display: block; width: 206px; top: 235px; left: 412px; overflow:
visible;">
<div id="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_DropDown"
class="RadComboBoxDropDown RadComboBoxDropDown_Telerik " style="width: 204px; display: block;
top: 0px; visibility: visible; transition: none 0s ease 0s;">

<div class="rcbScroll rcbWidth" style="height: 126px;">
<ul class="rcbList">
<li class="rcbHovered">Forespørgsel til Energinet</li>
<li class="rcbItem">Måledata</li>
<li class="rcbItem">Målepunkt eller ad hoc spørgsmål</li>
<li class="rcbItem">Orientering til alle</li>
<li class="rcbItem">Priser</li>
<li class="rcbItem">Pristilknytninger</li>
</ul>
</div>
</div>
</div>

I am doing this:
HTMLDoc.getElementById("ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_Arrow").Click 'This is an arrown that opens up the dropdown list
HTMLDoc.getElementById("ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_DropDown").Focus
'HTMLDoc.getElementById("ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_DropDown").Value = "Målepunkt eller ad hoc spørgsmål"
HTMLDoc.getElementById("ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_DropDown").FireEvent ("onchange")

The value part doesn't work as this is not an attribute. I think I need to go down the rcb list and select/click on item 3? How?
 
Upvote 0
Sourcode for dropdown:
<div class="rcbSlide" style="z-index: 6000; display: block; width: 206px; top: 235px; left: 412px; overflow:
visible;">
<div id="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_DropDown"
class="RadComboBoxDropDown RadComboBoxDropDown_Telerik " style="width: 204px; display: block;
top: 0px; visibility: visible; transition: none 0s ease 0s;">

<div class="rcbScroll rcbWidth" style="height: 126px;">
<ul class="rcbList">
<li class="rcbHovered">Forespørgsel til Energinet</li>
<li class="rcbItem">Måledata</li>
<li class="rcbItem">Målepunkt eller ad hoc spørgsmål</li>
<li class="rcbItem">Orientering til alle</li>
<li class="rcbItem">Priser</li>
<li class="rcbItem">Pristilknytninger</li>
</ul>
</div>
</div>
</div>

I am doing this:
HTMLDoc.getElementById("ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_Arrow").Click 'This is an arrown that opens up the dropdown list
HTMLDoc.getElementById("ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_DropDown").Focus
'HTMLDoc.getElementById("ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_DropDown").Value = "Målepunkt eller ad hoc spørgsmål"
HTMLDoc.getElementById("ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_DropDown").FireEvent ("onchange")

The value part doesn't work as this is not an attribute. I think I need to go down the rcb list and select/click on item 3? How?
xyz = ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_DropDown
 
Upvote 0
This is the xpath alternative
Command = Click
xpath=//*[@id="ctl00_MainContent_CreateWebForm__SubjectComboBox_ComboBox_DropDown"]/div/ul/li[3]

This works but I need a VBA alternative to this - how can this be done in VBA?
 
Upvote 0
Not a standard dropdown then, but a Telerik web control. As I said, look at the events on the element and maybe its parents and simulate them with dispatchEvent.
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,239
Members
448,951
Latest member
jennlynn

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