PDA

View Full Version : [SOLVED:] Unable to select a value in a DropDown List in a Webpage using VBA



nastyJ
08-24-2016, 08:45 AM
Hello all,

I am trying to select a value in a dropdown list I have on an intranet webpage.

The Html code of the dropdown list is as follows:



<SELECT name="findby" tabIndex="1" class="selectCode" onchange="changeSearchBy();document.filteringForm.criterion.focus();return true;" >
<OPTION selected value=Name>Name</OPTION>
<OPTION value=RICOS>Ricos</OPTION>
<OPTION value=SGC>SGC</OPTION>
<OPTION value=DIG>DIG</OPTION>
<OPTIONvalue=RMC>RMC</OPTION>
<OPTION value=REUTERS>REUTERS</OPTION>
</SELECT]>


Now my code is as follows:


Sub navigateGCE()
Dim ie As New InternetExplorer
Dim IEDoc As HTMLDocument
Dim htmlSelectElem As Variant
Dim elementHtml As Object

ie.Visible = True
'
ie.navigate "MY INTRANET WEBPAGE"

Do While ie.Busy Or Not ie.readyState = READYSTATE_COMPLETE
DoEvents
Loop


Set IEDoc = ie.document


Set htmlSelectElem = IEDoc.getElementsByTagName("findby")
htmlSelectElem.selectedIndex = 2
IEDoc.parentWindow.execScript "changeSearchBy()", "JavaScript"

Set ie = Nothing
Set IEDoc = Nothing


End Sub


It is to be noted that I have declared htmlSelectElem as Variant, using the debugger I see the type of this object is DispHTMLElementCollection.

When I execute the code I have :

Run time error 438, Object doesn't support this property or method

I have tried to use getElementsByName instead but it doesn't work.

Do you know how I can fix this?

Many thanks

nastyJ
08-25-2016, 05:44 AM
I found the issue guys, it was because I had an other element called "finbdy" on my webpage.

So I had to do it like this:

Set iCollection = IEDoc.all("findby")
Set generic = iCollection(1)
generic.Value = "RICOS"

Thanks