vba code to get element from label name

Pankil

New Member
Joined
Aug 1, 2019
Messages
16
Hi,

I need to select option "Assigned to anyone" from below code. it looks like drop down list. HTML for it is as below

HTML:
<select class="form-control ng-pristine ng-untouched ng-valid ng-not-empty" ng-model="viewOptions.assignedToMe" ng-options="opt.value as opt.label for opt in ::assignedToMeOptions" ng-change="setAssignedToMeOption(viewOptions.assignedToMe)"><option label="Assigned to me" value="boolean:true" selected="selected">Assigned to me</option><option label="Assigned to anyone" value="boolean:false">Assigned to anyone</option></select>

Thanks in advance!!!!!!!!!
 
Last edited by a moderator:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Add this to your code (requires references to MS Internet Controls and HTML Object Library):
Code:
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim selectElement As HTMLSelectElement

'After IE navigation and page has loaded
Set HTMLdoc = IE.document
Set selectElement = HTMLdoc.getElementsByTagName("SELECT")(0)  'assumes first select element
selectElement.selectedIndex = 1
 
Upvote 0
Add this to your code (requires references to MS Internet Controls and HTML Object Library):
Code:
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim selectElement As HTMLSelectElement

'After IE navigation and page has loaded
Set HTMLdoc = IE.document
Set selectElement = HTMLdoc.getElementsByTagName("SELECT")(0)  'assumes first select element
selectElement.selectedIndex = 1

Hey John, Many thanks it works and select the desired drop down.
one more query is that it is not fireing desired event. The data which is there is not refreshing on selection. when i do manual selection, it is updating but not with the selection by below code...
 
Upvote 0
Which event is it meant to fire? Use your DOM browser to see which event(s) are assigned to the select element and fire them using FireEvent or dispatchEvent

Just guessing - maybe it needs the change event. Try either:

Code:
    selectElement.FireEvent "onchange"
Or:
Code:
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
        Dim changeEvent As DOMEvent
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
        Dim changeEvent As Object
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If

    'After page has completely loaded
    Set changeEvent = HTMLdoc.createEvent("HTMLEvents")
    changeEvent.initEvent "change", True, False
    selectElement.dispatchEvent changeEvent
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,620
Latest member
dsubash

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