在IE输入字段上触发事件时出现VBA错误"change"



我正在尝试使用VBA访问使用Internet Explorer的内部网页,打开搜索窗口,输入搜索参数并运行搜索。我的代码将单击所有按钮,找到所有文本框并输入参数。但是,它无法识别使用".value"输入的任何参数(即objElement.value = "1234567"(。我已经根据我在这个论坛和其他一些论坛上读到的内容编写了代码,但显然,我仍然缺少一些东西。当它尝试触发"change"事件时,我收到错误 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.

网页代码

"change"事件是否会在IE中没有VBA的情况下触发两者的更新?如果"更改"事件仅与输入框相关联,并且不会在没有VBA的情况下触发下拉列表的更新,那么我认为它也无法与VBA一起使用。此外,"变化"事件是什么样的?

我做了一个如下所示的演示,似乎可以很好地触发"更改"事件,您可以尝试检查一下。

网页代码:

<!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

相关内容

  • 没有找到相关文章

最新更新