excel - 输入框填满数据后,Excel VBA触发网页中的事件
问题描述
我正在尝试在网页中填充一个输入框,该输入框实际上触发事件会导致自动更新一些隐藏字段。
网页背后的代码
<INPUT onchange=iCFieldChanged(this); tabIndex=1017 onkeypress="AllowOnly(event, '[()0-9-+]','');" onfocus="tbInitialValue=this.value;g_fFieldValue = this.value;g_fFieldId = this.id;" onblur="if(tbInitialValue!=this.value && Page_IsValid){executingPostBack=true;__doPostBack('dnn:ctr366:ClaimPhysicianInformation:_ctl2:Datapanel:_ctl0:_ctl5:ClaimRecipient:ds_ClaimRecipient:mb_ds_ClaimRecipient', '');}g_fFieldId = null;" id=dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient onpaste="return true;" style="TEXT-ALIGN: right" maxLength=10 size=11 value=9468381692 name=dnn:ctr366:ClaimPhysicianInformation:_ctl2:Datapanel:_ctl0:_ctl5:ClaimRecipient:ds_ClaimRecipient:mb_ds_ClaimRecipient>
上面代码的第一个输入标记行上有 5 个事件侦听器 - onblur
, onkeypress
, onfocus
, onchange
, onpaste
. 我可以将值传递给输入框,但关联的事件不会被释放。
代码尝试(没有错误,但未调度事件)
ie.Document.getElementById("dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient").Value = memberId
'tried with all fire event combinations but nothing works
ie.Document.getElementById("dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient").FireEvent ("onkeypress")
也试图调度事件,但它显示错误
Set srchbtn = ie.Document.getElementById("dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient")
srchbtn.Value = memberId
'getting error in next line
Set event_onChange = ie.Document.createEvent("HTMLEvents")
event_onChange.initEvent "keypress", True, False
srchbtn.dispatchEvent event_onChange
还尝试了如下按键事件
Set objEvent = IE.Document.createEvent("keyboardEvent")
objEvent.initEvent "keypress", True, False
IE.Document.getElementById("dnn_ctr366_ClaimPhysicianInformation__ctl2_Datapanel__ctl0__ctl5_ClaimRecipient_ds_ClaimRecipient_mb_ds_ClaimRecipient")(0).dispatchEvent objEvent
但出现同样的错误
错误:运行时错误“438”:对象不支持此属性或方法
因为它是一个安全的网页,所以我不能分享复制代码的 url,请指导我进行进一步的处理
解决方案
在没有 VBA 的情况下,您的输入事件是否可以在 IE 中正确触发?我试图分派该onchange
事件并且它起作用了。我按照本文中的方式进行。您可以参考下面我的简单示例:
输入框:<input name="q" onchange="alert('aaa');"/>
VBA代码:
Sub LOADIE()
Set ieA = CreateObject("InternetExplorer.Application")
ieA.Visible = True
ieA.navigate "http://somewebsite"
Do Until ieA.readyState = 4
DoEvents
Loop
Set doc = ieA.Document
Set Search = doc.getElementsByName("q")(0)
Search.Value = "VBA"
Dim event_onChange As Object
Set event_onChange = ieA.Document.createEvent("HTMLEvents")
event_onChange.initEvent "change", True, False
Search.dispatchEvent event_onChange
ieA.Quit
Set ieA = Nothing
End Sub
上面的函数也适用于keypress
事件。关于您收到的错误,您可以查看此线程以获取更多信息。
推荐阅读
- elasticsearch - Elasticsearch 的问题 - 即使插入 API 只运行一次,Elasticsearch 也会插入两条或更多记录
- flutter - Flutter:问题/答案列表每页查看一项(例如 Taxfix)
- if-statement - Makeifle:如果定义块中的条件块
- python - DJANGO ORM - 在案例中使用时间列表更新/添加字段
- bash - 在 wget 中处理超时
- java - java.sql.SQLException:找不到合适的驱动程序(ide android studio)
- javascript - 如何在三个js中将X标记放在框的顶部
- flutter - Flutter:如何创建一个常量/最终数组,以便我可以使用 switch 语句中的值?
- r - 不处理 r 中的 trycatch
- python - 通过 python-social-auth 创建用户配置文件