Select 使用来自 IE11 保管箱的 VBA 的值
Select value using VBA from IE11 dropbox
我到处寻找解决方案,但没有任何效果。这是我的代码:
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
问题是我无法让 dispatchevent 工作。如果我使用 f8 单步执行我的代码,我可以看到下拉框更改为 CAD,但是当我提交表单时,它会变回原始值。该页面实际上从未刷新。我已经尝试了这个网站上建议的每一个解决方案,一切都与下面完全一样。仅供参考,我正在使用 IE11。
PS 抱歉,我无法将 link 提供给页面
更新:我现在认为是提交表单事件现在不起作用。如果我删除下拉菜单并单击复选框,然后尝试 运行 带有
的表单
'load data
Set GetMTM = IE.document.forms(0)
GetMTM.submit
它不会加载表单。但是,如果我自己去点击 Go 按钮,它就可以工作了..
这是提交按钮和复选框 HTML 的
<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"/> </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"/> </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"/> </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"/> </TD>
<TD align="left" valign="center">Theirs</TD>
编辑:这是保管箱的 html 代码:
<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>
我自己为所有关心的人解决了这个问题...
问题出在我尝试提交表单时...与下拉列表无关。
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
我到处寻找解决方案,但没有任何效果。这是我的代码:
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
问题是我无法让 dispatchevent 工作。如果我使用 f8 单步执行我的代码,我可以看到下拉框更改为 CAD,但是当我提交表单时,它会变回原始值。该页面实际上从未刷新。我已经尝试了这个网站上建议的每一个解决方案,一切都与下面完全一样。仅供参考,我正在使用 IE11。
PS 抱歉,我无法将 link 提供给页面
更新:我现在认为是提交表单事件现在不起作用。如果我删除下拉菜单并单击复选框,然后尝试 运行 带有
的表单'load data
Set GetMTM = IE.document.forms(0)
GetMTM.submit
它不会加载表单。但是,如果我自己去点击 Go 按钮,它就可以工作了.. 这是提交按钮和复选框 HTML 的
<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"/> </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"/> </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"/> </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"/> </TD>
<TD align="left" valign="center">Theirs</TD>
编辑:这是保管箱的 html 代码:
<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>
我自己为所有关心的人解决了这个问题...
问题出在我尝试提交表单时...与下拉列表无关。
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