htmlvbainternet-explorer-11getelementbyiddispatchevent

Select value using VBA from IE11 dropbox


I have looked everywhere for a solution and nothing works. Here is my code:

Sub GetData()


Dim IE As Object
Dim ccy As Object
Dim ccy1 As Object
Dim allstatus As Object
Dim GetMTM As Object

Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = True
Application.DisplayAlerts = False

'go to MTM report
IE.navigate "www.mylink.com"

'check for good connection
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
'wait for window to open
Application.Wait (Now + TimeValue("0.00:01"))

'send login information
SendKeys "blah", True
SendKeys "{TAB}", True
SendKeys "blah"
SendKeys "{TAB}", True
SendKeys "blah"
SendKeys "{ENTER}", True

'check for good connection
Do Until Not IE.Busy And IE.readyState = 4
DoEvents
Loop
'wait for window to open
Application.Wait (Now + TimeValue("0.00:01"))

'all status
Set allstatus = IE.document.getelementbyid("allstatus1")
allstatus.Click

Set ccy = IE.document.createevent("HTMLEvents")
ccy.initevent "change", True, False
Set ccy1 = IE.document.getelementbyid("currency")

ccy1.selectedindex = 9
ccy1.dispatchevent ccy 'nothing happens here!!!

'load data
Set GetMTM = IE.document.forms(0)
GetMTM.submit

End Sub

The issue is that i can't get the dispatchevent to work. If i step through my code using f8, i can see the drop box change to CAD but then when i submit the form, it changes back to the original value. The page never actually refreshes. I have tried every solution suggested on this site and everything works the exact same as below. FYI i am using IE11.

PS i apologize but i can't provide the link to the page

UPDATE: I now believe it is the submit form event that is not working now. If i remove for the dropdown and the checkbox click, and just try to run the form with

'load data
Set GetMTM = IE.document.forms(0)
GetMTM.submit

It won't load the form. But if i go and click on the Go button myself it works.. Here's the HTML for the submit button and check boxes

                <TD align="right" valign="center"><input id="matchedstatus1" name="matchedstatus" style="font-family: Verdana, Helvetica, sans-serif; font-size: 12px" onclick="javascript:changeStatus(markToMarketBean, this)" type="checkbox" value="true" checked="checked"/><input type="hidden" name="_matchedstatus" value="on"/>&nbsp;</TD>
                <TD align="left" valign="center">Matched</TD>
                <TD><IMG src="/images/gtss/neutral/spacer.gif" width="12"
                    height="1"></TD>
                <TD align="right" valign="center"><input id="oursource1" name="oursource" style="font-family: Verdana, Helvetica, sans-serif; font-size: 12px" onclick="javascript:changeSource(markToMarketBean, this)" type="checkbox" value="true" checked="checked"/><input type="hidden" name="_oursource" value="on"/>&nbsp;</TD>
                <TD align="left" valign="center">Ours</TD>
                <TD rowspan="2"><IMG src="/images/gtss/neutral/spacer.gif"
                    width="10" height="1"></TD>
                <TD align="left" valign="middle" rowspan="2"><input type="submit"
                    name="go" value='Go' 
                    style="font-family: Verdana, Helvetica, sans-serif; font-size: 12px">
                </TD>
            </TR>
            <TR>
                <TD align="right" valign="center"><input id="allstatus1" name="allstatus" style="font-family: Verdana, Helvetica, sans-serif; font-size: 12px" onclick="javascript:changeStatus(markToMarketBean, this)" type="checkbox" value="true"/><input type="hidden" name="_allstatus" value="on"/>&nbsp;</TD>
                <TD align="left" valign="center">All</TD>
                <TD><IMG src="/images/gtss/neutral/spacer.gif" width="12"
                    height="1"></TD>
                <TD align="right" valign="center"><input id="theirsource1" name="theirsource" style="font-family: Verdana, Helvetica, sans-serif; font-size: 12px" onclick="javascript:changeSource(markToMarketBean, this)" type="checkbox" value="true"/><input type="hidden" name="_theirsource" value="on"/>&nbsp;</TD>
                <TD align="left" valign="center">Theirs</TD>

EDIT: Here's the html code for the dropbox:

<select id="currency" name="currency" style="font-family: Verdana, Helvetica, sans-serif; font-size: 12px;">
            <option value="AED">AED</option><option value="ARS">ARS</option><option value="AUD">AUD</option><option value="BDT">BDT</option><option value="BGL">BGL</option><option value="BGN">BGN</option><option value="BMD">BMD</option><option value="BOB">BOB</option><option value="BRL">BRL</option><option value="CAD">CAD</option><option value="CHF">CHF</option><option value="CLP">CLP</option><option value="CNY">CNY</option><option value="COP">COP</option><option value="CYP">CYP</option><option value="CZK">CZK</option><option value="DKK">DKK</option><option value="ECS">ECS</option><option value="EEK">EEK</option><option value="EGP">EGP</option><option value="EUR">EUR</option><option value="FIM">FIM</option><option value="GBP">GBP</option><option value="GHC">GHC</option><option value="HKD">HKD</option><option value="HRK">HRK</option><option value="HUF">HUF</option><option value="IDR">IDR</option><option value="ILS">ILS</option><option value="INR">INR</option><option value="ISK">ISK</option><option value="JOD">JOD</option><option value="JPY">JPY</option><option value="KES">KES</option><option value="KRW">KRW</option><option value="KWD">KWD</option><option value="KZT">KZT</option><option value="LBP">LBP</option><option value="LKR">LKR</option><option value="LTL">LTL</option><option value="LVL">LVL</option><option value="MAD">MAD</option><option value="MUR">MUR</option><option value="MXN">MXN</option><option value="MYR">MYR</option><option value="NGN">NGN</option><option value="NOK">NOK</option><option value="NZD">NZD</option><option value="PEN">PEN</option><option value="PGK">PGK</option><option value="PHP">PHP</option><option value="PKR">PKR</option><option value="PLN">PLN</option><option value="ROL">ROL</option><option value="RUB">RUB</option><option value="SEK">SEK</option><option value="SGD">SGD</option><option value="SIT">SIT</option><option value="SKK">SKK</option><option value="THB">THB</option><option value="TND">TND</option><option value="TRL">TRL</option><option value="TRY">TRY</option><option value="TWD">TWD</option><option value="TZS">TZS</option><option value="UAH">UAH</option><option value="USD" selected="selected">USD</option><option value="VEB">VEB</option><option value="VND">VND</option><option value="ZAR">ZAR</option><option value="ZWD">ZWD</option>

Solution

  • I figured it out myself for anyone who cares...

    The issue was when I was trying to submit the form... had nothing to do with the drop down.

    Sub GetData()
    
    Dim IE As New InternetExplorer
    Dim HTMLdoc As HTMLDocument
    
    Dim allstatus As Object
    Dim GetMTM As Object
    
    Set IE = New InternetExplorer
    
    IE.Visible = True
    Application.DisplayAlerts = False
    
    'go to MTM report
    IE.navigate "wwww.mylink.com"
    
    'check for good connection
    Do Until Not IE.Busy And IE.readyState = 4
    DoEvents
    Loop
    'wait for window to open
    Application.Wait (Now + TimeValue("0.00:01"))
    
    'send login information
    SendKeys "blah", True
    SendKeys "{TAB}", True
    SendKeys "blah"
    SendKeys "{TAB}", True
    SendKeys "blah!"
    SendKeys "{ENTER}", True
    
    'check for good connection
    Do Until Not IE.Busy And IE.readyState = 4
    DoEvents
    Loop
    'wait for window to open
    Application.Wait (Now + TimeValue("0.00:01"))
    
    'all status
    Set allstatus = IE.Document.getElementById("allstatus1")
    allstatus.Click
    
    Dim evt As Object
    Dim ccy As Object
    
    Set evt = IE.Document.createevent("HTMLEvents")
    evt.initEvent "change", True, False
    Set ccy = IE.Document.getElementById("currency")
    ccy.Click
    ccy.Value = "CAD"
    ccy.dispatchEvent evt
    
    '========this was the line i needed to fix!!!===========
    'load data
    IE.Document.getElementsByName("go")(0).Click
    '========================================================
    
    'check for good connection
    Do Until Not IE.Busy And IE.readyState = 4
    DoEvents
    Loop
    'wait for window to open
    Application.Wait (Now + TimeValue("0.00:01"))
    
    'download data
    IE.Document.getElementsByName("download")(0).Click
    
    Application.DisplayAlerts = True
    
    End Sub