html - 在 IE 输入字段上触发“更改”事件时出现 VBA 错误
问题描述
我正在尝试使用 VBA 使用 Internet Explorer 访问内部网页,打开搜索窗口,输入搜索参数并运行搜索。我的代码将单击所有按钮,找到所有文本框并输入参数。但是,它不能识别使用“.value”输入的任何参数(即,objElement.value = “1234567”)。我已经根据我在这个论坛和其他一些人中阅读的内容编写了代码,但显然,我仍然遗漏了一些东西。当它尝试触发“更改”事件时,我收到错误 438 - 对象不支持此属性或方法,或错误 5 - 无效的过程调用或参数。该错误取决于我是使用 dispatchEvent 还是 FireEvent 以及我是使用集合还是集合中的元素作为对象。
不幸的是,这是访问内部站点,因此您将无法运行代码。我希望有人可以看看并指出我正确的方向。
Dim EPA As String
Dim EPANumb As Integer
Dim EPAList() As String
Dim PrjCnt As Long
Dim PrjList As String
Dim WS As Worksheet
Dim IE As InternetExplorerMedium
Dim URL As String
Dim HWNDSrc As Long
Dim objCollection As Object
Dim objElement As Object
Dim objEvent As Object
Dim oHtml As HTMLDocument
Dim HTMLtags As IHTMLElementCollection
'Pull list of project numbers and enter into array
fn = ThisWorkbook.Name
EPANumb = WorksheetFunction.CountA(Worksheets("Instructions").Range("B:B"))
EPANumb = EPANumb - 2
ReDim EPAList(EPANumb)
For PrjCnt = 0 To EPANumb
If EPANumb > 0 Then
EPAList(PrjCnt) = "'" & Worksheets("Instructions").Cells((PrjCnt + 2), 2).Value & "'"
Else
EPAList(PrjCnt) = Worksheets("Instructions").Cells((PrjCnt + 2), 2).Value
End If
Next
PrjList = Join(EPAList, ",")
'Open IE and navigate to URL
Set IE = New InternetExplorerMedium
IE.Visible = True 'only if you want to see what is happening
URL = "http://anysite/SnakeEyes/grid.html"
IE.navigate URL
'need If statement in case user needs to do sign on.
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.Busy = True Or IE.ReadyState <> 4: DoEvents: Loop
Application.Wait (Now + TimeValue("0:00:05"))
'Webpage Loaded
Application.StatusBar = URL & " loaded"
Set oHtml = IE.document
'HWNDScr = IE.HWND
'SetForegroundWindow HWNDScr
'Open search box and reset search parameters
oHtml.getElementById("search_grid_c_top").Click
'IE.Document.getElementById("fbox_grid_c_reset").Click
Set objEvent = oHtml.createEvent("HTMLEvents")
objEvent.initEvent "change", True, False ' **** NEW ****
Set HTMLtags = oHtml.getElementsByTagName("select")
For i = 0 To HTMLtags.Length - 1
If HTMLtags(i).className = "selectopts" Then
If EPANumb > 0 Then
HTMLtags(i).Value = "in"
Else
HTMLtags(i).Value = "eq"
End If
' HTMLtags.dispatchEvent objEvent ' **** NEW ****
Exit For
End If
Next i
Set objEvent = oHtml.createEvent("HTMLEvents")
objEvent.initEvent "change", True, False ' **** NEW ****
Set HTMLtags = oHtml.getElementsByTagName("input")
For i = 1 To HTMLtags.Length - 1
If HTMLtags(i).ID > "jqg" And HTMLtags(i).ID < "jqh" Then
HTMLtags(i).Focus
HTMLtags(i).Value = PrjList
Exit For
End If
Next i
HTMLtags(i).dispatchEvent objEvent ' **** NEW ****
'HTMLtags(i).FireEvent objEvent ' **** NEW **** DispatchEvent gives an error 438.
'FireEvent gives an error 5 if using the (i); error 438 without it.
解决方案
“更改”事件是否会触发 IE 中没有 VBA 的更新?如果“更改”事件仅与输入框相关联并且不会触发没有 VBA 的下拉列表的更新,那么我认为它也不适用于 VBA。此外,“改变”事件是怎样的?
我做了一个像下面这样的演示,似乎“更改”事件可以很好地触发,您可以尝试检查一下。
HTML 代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<table>
<tbody>
<tr>
<td class="operators">
<select class="selectopts">
<option value="eq">equal</option>
<option value="in">is in</option>
<option value="ni">is not in</option>
</select>
</td>
<td class="data">
<input class="input-elm" id="jqg1" role="textbox" type="text" size="10" value="'1006191'"/>
</td>
</tr>
</tbody>
</table>
<script>
var select = document.getElementsByClassName('selectopts')(0);
var input = document.getElementById('jqg1');
input.addEventListener('change', updateValue);
function updateValue() {
select.value = "in";
}
</script>
</body>
</html>
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.getElementByID("jqg1")
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
推荐阅读
- java - 如何使用 Struts 2 jQuery 解决错误设置日期
- sql - 执行递归查询时检测到循环
- c - 当 x=0x80000000, y = 1(32 位补码)时,为什么 `x - y <= x` 为真?
- image - 如何在初始屏幕中使用多个图像进行反应原生
- android - Android Auto Desktop Head Unit (DHU) Hello World 应用程序已启动,但 DHU 中没有可用的启动器图标
- html - react-masonry-component 不在父 div 中居中
- pytorch - 如何在使用 ONNX 推理会话时通过传递“标签”来获得语言建模损失?
- ios - 具有相同基类单元的委托调用的多个控制器中的 DelegateProtocol 问题
- r - 枢轴更长的包机标题
- powerbi - 计算产品销售额大于 0 的月份数