What a challenge! I am certainly not the least experienced person, but certainly not the best either.
Already when I read your text, it was clear to me that these are HTML events. I already knew the site, but I didn’t know what to expect. I invested several hours now, but in the end I cracked it.
The following macro with the additional Sub() solves your problem. For more information, please refer to the comments in the macro. The solution was very difficult for me, but I did not learn anything, because all knowledge was there. But not in this way.
In the end everything seems to be simple. You wouldn’t believe how many combinations of events I tried.
Take this macro, it works:
Sub OandaCurrencyConverter() Dim ie As Object Dim doc As Object Dim nodeCurrencyDropdown As Object Dim nodeAllCurrencies As Object Dim nodeOneCurrency As Object Dim starttime As Double Dim Curr As String Dim row As Long Dim i As Byte Dim leftRightIdentifier As String Dim myws As Worksheet starttime = Timer Set myws = ThisWorkbook.Worksheets("Ui") myws.Range(myws.Cells(4, 9), myws.Cells(myws.Rows.Count, 9)).ClearContents 'Delete previous results 'Initialize Internet Explorer, set visibility, 'call URL and wait until page is fully loaded Set ie = CreateObject("internetexplorer.application") ie.Visible = False ie.navigate "https://www1.oanda.com/currency/converter/" Do Until ie.readyState = 4: DoEvents: Loop Set doc = ie.document 'Get results Do While myws.Cells(4 + row, 4).Value <> "" 'ENTERING VALUE TO BE CONVERTED 'If this value is entered first, the desired result is calculated 'automatically when the currencies are set in the dropdowns doc.getElementById("quote_amount_input").Value = myws.Cells(4 + row, 8).Value 'ENTERRING CURRENCIES For i = 0 To 1 If i = 0 Then 'Left currency leftRightIdentifier = "quote" Curr = myws.Cells(4 + row, 5).Value Else 'Right currency leftRightIdentifier = "base" Curr = myws.Cells(4 + row, 7).Value End If 'Get the needed dropdown Set nodeCurrencyDropdown = doc.getElementById(leftRightIdentifier & "_currency_list_container") 'Generate node collection of all currencies in dropdown Set nodeAllCurrencies = nodeCurrencyDropdown.getElementsByClassName("ltr_list_item") 'Search the wanted currency in the single nodes For Each nodeOneCurrency In nodeAllCurrencies If InStr(1, nodeOneCurrency.innerText, Curr) > 0 Then Call TriggerEvent(doc, nodeOneCurrency, "mouseover") nodeOneCurrency.Click Exit For End If Next nodeOneCurrency Next i 'Give a little time to calculate and get the result Application.Wait (Now + TimeValue("0:00:02")) myws.Cells(4 + row, 9).Value = doc.getElementById("base_amount_input").Value * 1 'Next row row = row + 1 Loop 'Clean up ie.Quit Set ie = Nothing Set doc = Nothing Set nodeCurrencyDropdown = Nothing Set nodeAllCurrencies = Nothing Set nodeOneCurrency = Nothing 'Show needed time MsgBox "Currencies have been converted" & vbNewLine & "Time Taken - " & Format((Timer - starttime) / 86400, "hh:mm:ss") End Sub
And this Sub() to trigger HTML events:
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String) Dim theEvent As Object htmlElementWithEvent.Focus Set theEvent = htmlDocument.createEvent("HTMLEvents") theEvent.initEvent eventType, True, False htmlElementWithEvent.dispatchEvent theEvent End Sub