Excel vba to select item from dropdown on webpage

gedwarren

New Member
Joined
Sep 24, 2012
Messages
1
I'm having trouble getting the syntax right to select an item from a dropdown on a webpage. It's to select gender. I think it's something to do with it using javascript. When I do Inspect element on the dropdown I get the following:

Code:
<table width="100%" style="z-index: auto;" cellspacing="0" cellpadding="0" oldz="auto">
<tbody style="z-index: auto;" oldz="auto"><tr style="z-index: auto;" oldz="auto">
<td style="width: 100%; z-index: auto;" oldz="auto">
<div class="FBox" style="width: auto; position: relative; z-index: auto;" oldz="auto" outerdiv="Y">
<input name="VX_Gender" id="VX_Gender" type="hidden" value="M">
<div class="EditDiv EditDropDown" id="VX_Gender_ext" role="textbox" contenteditable="true" spellcheck="false" ondrop="return false" origval="Male" hig="1" max="-1" strictselection="Y" focstyle="EditDropDownFoc" isfilter="Y" filterbox="Y" val2="Male" LastTouchType="Y">Male</div>
********>initDiv('VX_Gender');*********>
</div>
</td>
<td style="white-space: nowrap;"></td>
</tr>
</tbody>
</table>

When I do the same on a dropdown item I get:

Code:
<div class="filtermenu" style="left: 271.76px; top: 252.5px; width: 122px; height: auto; margin-top: 0px; display: none; z-index: 1698; -ms-overflow-y: auto; max-height: 67px;" *******="return filterPopupClick( event );" hovermenu="Y" shrink="Y" unselectable="on" lastfilter="Male">
<div class="bcrow rsel" ***********="return fRowHov(this);" value="F" origva="Female" hc="#FFFFFF" fkey="F" frow="Y" fIdx="2">
Fe
<span class="fsr">male</span>
</div>

<div class="bcrow" ***********="return fRowHov(this);" value="I" origva="Indeterminate" hc="#FFFFFF" fkey="I" frow="Y" fIdx="-1">Indeterminate</div>
<div class="bcrow" ***********="return fRowHov(this);" value="M" origva="Male" hc="#FFFFFF" fkey="M" frow="Y" fIdx="0"><span class="fsr">Male</span></div>
<div class="bcrow" ***********="return fRowHov(this);" value="U" origva="Unborn" hc="#FFFFFF" fkey="U" frow="Y" fIdx="-1">Unborn</div>
</div>

I've tried various formats in my code but none seem to work. Any help would be much appreciated.


Code:
Sub gender()

Dim URL, PER_ID As String
Dim objShell, ie  As Object
Dim ele As IHTMLElement
'Determine if a specific instance of IE is already open.
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
    On Error Resume Next    'Sometimes more web pages are counted than are open
    URL = objShell.Windows(x).******************
    If URL = "[URL]https://protocolshef.syhapp.com:51020/web/faorc.htm[/URL]" Then   'Identify the existing web page
        Set ie = objShell.Windows(x)
        Exit For
    Else
    End If
Next
For Each ele In ie.document.getElementsByTagName("div")
    If ele.ID = "VX_Gender_ext" Then
        ele.innerText = "Female"
        ele.Val2 = "Female"
        Call ie.document.parentWindow.execScript("initDiv('VX_Gender');", "JavaScript")
    End If
Next
For Each ele In ie.document.getElementsByName("VX_Gender")
    ele.Value = "F"
    ele.origval = "F"
Next
For Each ele In ie.document.getElementsByTagName("div")
    If ele.ID = "VX_Gender_ext" Then
        ele.innerText = "Female"
        ele.Val2 = "Female"
        Call ie.document.parentWindow.execScript("initDiv('VX_Gender');", "JavaScript")
    End If
Next
For Each ele In ie.document.getElementsByName("VX_Gender")
    ele.Type = "text"
    ele.Value = "F"
    ele.origval = "F"
Next


End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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