mbbarclay
asked on
vba interacting with HTMLselectelement with onchange property
I am trying to automate entries to a web page and I am getting stuck on a select element that contains an onchange property. When a selection is made by clicking on one of the option elements, new information is added to the webpage. When I try to automate this selection using vba, the onchange property does not seem to be activated.
Here is the source code for the HTMLselectelement I am trying to interact with.
<select id="rcat" name="rcat"
onchange="rr_changeHandler (this);">
<option value="oef">Element Forces
<option value="gpf">Grid Point Forces
<option value="balance">Grid Point Force Balance
<option value="oelop">Element-orie nted Forces
<option value="disp">Displacement Vectors
<option value="maxmin">Envelope Loads
<option value="stress">Envelope Stresses
<option value="maxd">Envelope Displacements
<option value="bdf">Bulk Data
<option value="compare">Comparison (Ratios)
</select></td>
I can select any option here, the only problem i have is triggering the onchange.
Here is the source code for the HTMLselectelement I am trying to interact with.
<select id="rcat" name="rcat"
onchange="rr_changeHandler
<option value="oef">Element Forces
<option value="gpf">Grid Point Forces
<option value="balance">Grid Point Force Balance
<option value="oelop">Element-orie
<option value="disp">Displacement Vectors
<option value="maxmin">Envelope Loads
<option value="stress">Envelope Stresses
<option value="maxd">Envelope Displacements
<option value="bdf">Bulk Data
<option value="compare">Comparison
</select></td>
I can select any option here, the only problem i have is triggering the onchange.
ASKER
Thanks for the response, but I am trying to automate the selection from this webpage using VBA. I have no control over how the HTML is written for the webpage.
This is what i have so far:
Dim IE As SHDocVw.InternetExplorer
Dim hCol As MSHTML.IHTMLElementCollect ion
Dim hSel As MSHTML.HTMLSelectElement
Set hCol = IE.document.getElementsByT agName("SE LECT")
For Each hSel In hCol
If hSel.Name = "rcat" Then
hSel.selectedIndex = 6
Exit For
End If
Next hSel
This will select the option element that I need, but I cannot figure out how to interact with the onchange call.
This is what i have so far:
Dim IE As SHDocVw.InternetExplorer
Dim hCol As MSHTML.IHTMLElementCollect
Dim hSel As MSHTML.HTMLSelectElement
Set hCol = IE.document.getElementsByT
For Each hSel In hCol
If hSel.Name = "rcat" Then
hSel.selectedIndex = 6
Exit For
End If
Next hSel
This will select the option element that I need, but I cannot figure out how to interact with the onchange call.
you may want to change
Dim hSel As MSHTML.HTMLSelectElement
to declare on global as
Private WithEvents hSel As MSHTML.HTMLSelectElement
Private Sub hSel_onchange()
End Sub
Dim hSel As MSHTML.HTMLSelectElement
to declare on global as
Private WithEvents hSel As MSHTML.HTMLSelectElement
Private Sub hSel_onchange()
End Sub
It is not been tested
Private WithEvents hSel1 As MSHTML.HTMLSelectElement
Private Sub hSel1_onchange()
End Sub
Dim IE As SHDocVw.InternetExplorer
Dim hCol As MSHTML.IHTMLElementCollect ion
Dim hSel As MSHTML.HTMLSelectElement
Set hCol = IE.document.getElementsByT agName("SE LECT")
For Each hSel In hCol
If hSel.Name = "rcat" Then
hSel.selectedIndex = 6
set hSel1 = hSel
Exit For
End If
Next hSel
Private WithEvents hSel1 As MSHTML.HTMLSelectElement
Private Sub hSel1_onchange()
End Sub
Dim IE As SHDocVw.InternetExplorer
Dim hCol As MSHTML.IHTMLElementCollect
Dim hSel As MSHTML.HTMLSelectElement
Set hCol = IE.document.getElementsByT
For Each hSel In hCol
If hSel.Name = "rcat" Then
hSel.selectedIndex = 6
set hSel1 = hSel
Exit For
End If
Next hSel
ASKER
i figure it out. I had to add a fireevent to the selectelement
Dim IE As SHDocVw.InternetExplorer
Dim hCol As MSHTML.IHTMLElementCollect ion
Dim hSel As MSHTML.HTMLSelectElement
Set hCol = IE.document.getElementsByT agName("SE LECT")
For Each hSel In hCol
If hSel.Name = "rcat" Then
hSel.selectedIndex = 6
hSel.FireEvent "onchange", hSel
Exit For
End If
Next hSel
Dim IE As SHDocVw.InternetExplorer
Dim hCol As MSHTML.IHTMLElementCollect
Dim hSel As MSHTML.HTMLSelectElement
Set hCol = IE.document.getElementsByT
For Each hSel In hCol
If hSel.Name = "rcat" Then
hSel.selectedIndex = 6
hSel.FireEvent "onchange", hSel
Exit For
End If
Next hSel
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<html>
<head>
<script language="JavaScript">
function rr_changeHandler()
{
alert(rcat.options(rcat.se
}
</script>
</head>
<body>
<select id="rcat" name="rcat"
onchange="rr_changeHandler
<option value="oef">Element Forces
<option value="gpf">Grid Point Forces
<option value="balance">Grid Point Force Balance
<option value="oelop">Element-orie
<option value="disp">Displacement Vectors
<option value="maxmin">Envelope Loads
<option value="stress">Envelope Stresses
<option value="maxd">Envelope Displacements
<option value="bdf">Bulk Data
<option value="compare">Comparison
</select>
</body>
</html>