BrownBear
BrownBear

Reputation: 11

Select option using Excel Automation with Internet explorer

I have been trying for days. I can update the value of text boxes and click on option buttons however I am unable to select the option from a drop down list

To select the drop down option I have tried:

 myHTMLFrame2.all.Item("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO").Focus
    myHTMLFrame2.all.Item("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO").selectedIndex = 1
    myHTMLFrame2.all.Item("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO").FireEvent "onchange"

Nothing happens with the above

Dim myHTMLFrame2 As HTMLDocument
    Set myHTMLFrame2 = HTMLDoc.frames(3).document
    Dim elem As Object

    Set elem = myHTMLFrame2.document.getElementById("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO")
(results in "object not supported error")
    elem.Focus
    elem.selectedIndex = 2

This is the HTML Code:

<div id="QSGAA5V0GAFRWAOL34D8OK78W2ZUJO-answer-body">
                    <div class="select_holder select_jquery">
                        <select id="QSGAA5V0GAFRWAOL34D8OK78W2ZUJO" overwrite="1" level="0"
                            val="$escapeTool.html($!{answerValue})" required="true" questionid="QSGAA5V0GAFRWAOL34D8OK78W2ZUJO" totalorder="0" questiondefid="QDGAA5V0GAFRWAOL34D8OK78W2ZUJP"
                            responsetype="STATIC_MENU" autocomplete="off"
                            aria-activedescendant="" aria-labelledby="QSGAA5V0GAFRWAOL34D8OK78W2ZUJO-label" tabindex="0">
                            <option value=""></option>
                            <option value="New User(s)" ps="0" aria-selected="false">New User(s)</option>
                            <option value="Modify User Details or Applications" ps="1" aria-selected="false">Modify User Details or Applications</option>
                            <option value="Add/Modify User By Attachment" ps="2" aria-selected="false">Add/Modify User By Attachment</option>
                            <option value="clear">(clear)</option>
                        </select>
                    </div>
                </div>

Upvotes: 1

Views: 362

Answers (3)

BrownBear
BrownBear

Reputation: 11

@Qharr & @Deepak -- Thank You

I examined the element and turns out the code creates an "input" element at run time. In order to execute the event I am using send keys. Not very elegant but it works.

myHTMLFrame2.all.Item("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO_input").Focus
myHTMLFrame2.all.Item("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO_input").Value = "New User(s)"
myHTMLFrame2.all.Item("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO_input").Click
Application.SendKeys "{DOWN}"
Application.SendKeys "{DOWN}"
Application.SendKeys "~"

Upvotes: 0

Deepak-MSFT
Deepak-MSFT

Reputation: 11355

Try to make test with code below may help to solve the issue.

Code:

     Sub demo()

            Dim URL As String
            Dim IE As Object

            Set IE = CreateObject("InternetExplorer.Application")

            IE.Visible = True

            URL = "C:\Users\Administrator\Desktop\107.html"

            IE.Navigate URL

            Do While IE.ReadyState = 4: DoEvents: Loop   'Do While
            Do Until IE.ReadyState = 4: DoEvents: Loop   'Do Until

             IE.Document.GetElementByID("QSGAA5V0GAFRWAOL34D8OK78W2ZUJO").Value = "Modify User Details or Applications"

            Set IE = Nothing  


End Sub

Output in IE 11:

enter image description here

Upvotes: 0

QHarr
QHarr

Reputation: 84465

You could try attaching an event to the select element. I have targeted using tag and attribute in case id is dynamic. If id static then use that. If it is inside the iframe then add the appropriate syntax in front to access.

Dim dropDown As Object, onChange As Object
Set dropDown = ie.document.querySelector("select[overwrite='1']")
Set onChange = ie.document.createEvent("htmlevents")
onChange.initEvent "change", True, False
ie.document.querySelector("[value='New User(s)']").Selected = True
links.dispatchEvent onChange

Upvotes: 0

Related Questions