1
votes

I'm trying to trigger "on change" event upon selecting currency value from drop-down menu. This would update the mini spreadsheet on that page reflecting the value of the currency. However when changing the value through VBA jscript is not being triggered. I'm using IE to do this.

  Set objIE = New InternetExplorer
    objIE.Visible = True
    objIE.navigate "https://www.reuters.com/markets/currencies"
    Do While objIE.Busy = True Or objIE.readyState <> 4: DoEvents: Loop

    'Settlement Currency is Hard Coded to GBP
    SettCcy = "GBP"
    'Set Expression Currency
    expCcy = "EUR"
    'Set Target Currency
    targetCcy = "USD"

    On Error GoTo ResetIE

    If expCcy <> SettCcy And expCcy <> targetCcy Then

        'Set the rate refresher
        Set clicker = objIE.document.getElementsByClassName("CurrencyCalculator-currency-swap-2yw2I")(0)

        'Set the Expression Currency
        For Each o In objIE.document.getElementsByTagName("select")(1) 'Sets Expression Currency
            If o.Value = expCcy Then 
                o.Selected = True
                o.FireEvent "onchange"
                Exit For
            End If
        Next

All I need is that upon changing the value through VBA the page would update the same way as it would when doing it manually.

1
why do you have 3 currencies if doing a conversion? Also, what is settlement, expression and target in relation to what we see on the page?QHarr
Hi, 3 currencies are just for the testing. If I get this working, loop will be implemented to go through all of them in the spreadsheet. Settlement, target and expression will be taken from the sheetRadas

1 Answers

0
votes

Please try to use the dispatchEvent method to dispatch the change event to the select element, then trigger the onchange event.

Sample code as below (you could refer to it and modify it, it works well on my side):

Public Sub ClickTest()

    Dim  ie As Object, evtChange As Object

    Dim item As Object

    Set ie = CreateObject("InternetExplorer.Application")
    With ie
        .Visible = True
        .Navigate2 "<the website url>"

        While .Busy Or .readyState <> 4: DoEvents: Wend

        Set evtChange = .Document.createEvent("HTMLEvents")
        evtChange.initEvent "change", True, False

        'get the select element. Please note the index, it is starting from 0.
        Set item = ie.Document.getElementsByTagName("select")(0)

        expCcy = "EUR"

        'Set the Expression Currency
        For Each o In item 'Sets Expression Currency
            If o.Value = expCcy Then
                o.Selected = True
                o.dispatchEvent evtChange
                Exit For
            End If
        Next        
    End With
End Sub

The website resource like this:

<select id="ddlCurrency" onchange="ddlCurrencyChange()" >
    <option value="GBP">GBP</option>
    <option value="EUR">EUR</option>
    <option value="USD">USD</option>
</select>

<script>

    function ddlCurrencyChange() {
        document.getElementById("result").innerHTML += "change event triggered... <br /> selected value is " + document.getElementById("ddlCurrency").value + "<br/>";
    }
</script>