0

In Excel VBA change option in the HTML select tag, I used the following code to change options within the <select> tag:

For Each objOption In objIE.Document.GetElementsByTagName("table")(0).GetElementsByTagName("td")(tdNode).GetElementsByClassName("txt_input1")(0).Options
    If objOption.Value = SelQ Then
        objOption.Selected = True
        objIE.Document.GetElementsByTagName("table")(0).GetElementsByTagName("td")(tdNode).GetElementsByClassName("txt_input1")(0).OnChange
    Else
        objOption.Selected = False
    End If
Next

This seems to work for web sites with nested <table> tags, but the web site was updated without the tags, so, to compensate for finding the selected option, I used this:

For Each objOption In objIE.Document.getElementById("frmProduction").elements.namedItem("WQ").Options
    If objOption.Value = strVal Then
        objOption.Selected = True
        objIE.Document.getElementById("frmProduction").elements.namedItem("WQ").onchange
        Exit For
    Else
        objOption.Selected = False
    End If
Next

This is giving me the following error: Run-time error '5002': Application-defined or object-defined error

I used the above solution because it worked in another Internet Explorer application that used <frames> tags, so I modified it a little:

objIE.document.frames("DemographicsIFrame").document.GetElementByID("DropDownPayerID").value = PayerID
objIE.document.frames("DemographicsIFrame").document.GetElementByID("DropDownPayerID").onchange

I've tried to get around it with no success. I can get the selected option to change, but that's it. It won't update the page with required info related to the selected option. In the example above, that's what the onchange event was used for...to change the page contents after the PayerID was updated.

Any advice on how to make this work?

Lou
  • 389
  • 3
  • 20
  • 38

1 Answers1

0

We were actually able to come up with a solution:

For Each objOption In objIE.document.getElementById("frmProduction").elements.namedItem("WQ").Options
    If objOption.Value = strVal Then
        objOption.Selected = True
        Set evtFiroz = objIE.document.createEvent("HTMLEvents")
        evtFiroz.initEvent "change", False, True
        objIE.document.getElementById("WQ").dispatchEvent evtFiroz
        Exit For
    Else
        objOption.Selected = False
    End If
Next
Lou
  • 389
  • 3
  • 20
  • 38