Link to home
Start Free TrialLog in
Avatar of mbbarclay
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-oriented 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.
Avatar of EDDYKT
EDDYKT
Flag of Canada image

this works for me

<html>
<head>
 <script language="JavaScript">
   function rr_changeHandler()
   {
    alert(rcat.options(rcat.selectedIndex).text);
 
   }

 </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-oriented 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>
</body>
</html>
Avatar of mbbarclay
mbbarclay

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.IHTMLElementCollection
  Dim hSel As MSHTML.HTMLSelectElement

Set hCol = IE.document.getElementsByTagName("SELECT")

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
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.IHTMLElementCollection
  Dim hSel As MSHTML.HTMLSelectElement

Set hCol = IE.document.getElementsByTagName("SELECT")

For Each hSel In hCol
  If hSel.Name = "rcat" Then
  hSel.selectedIndex = 6
set hSel1 = hSel
   Exit For
  End If
  Next hSel
i figure it out.  I had to add a fireevent to the selectelement

Dim IE As SHDocVw.InternetExplorer
  Dim hCol As MSHTML.IHTMLElementCollection
  Dim hSel As MSHTML.HTMLSelectElement

Set hCol = IE.document.getElementsByTagName("SELECT")

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
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial