VBA For Each和With Block对象变量或With Block变量未设置错误91



我正在尝试使用VBA在Internet Explorer的网页中填充HTML表。我正在使用以下代码尝试输入一个值;字段具有唯一的名称:

Dim objShellWins As SHDocVw.ShellWindows
Dim objIE As InternetExplorer
Dim objDoc As Object
For Each objIE In objShellWins   'Look at IE & Windows Explorer objects in shell
With objIE
'Look for URL matching strURL
If (InStr(1, .LocationURL, [URL], vbTextCompare)) Then
Set objDoc = .Document
If (InStr(1, objDoc.Title, [Title], vbTextCompare)) Then
objDoc.getElementsByName([Name])(0).Value = "Value"
End If
End If
End With
Next

我在objDoc.getElementsByName行上得到错误91:Object Variable或With Block Variable Not Set,但是objDoc.Title返回了正确的字符串。有没有想过我该怎么解决这个问题?

objDoc不是导致错误的对象变量。getElementsByName返回NodeList对象(Nodes的集合(,getElementByName(...)(0)返回Node对象。这就是导致错误的对象"变量"(非常误导性,因为它根本不是变量(。

@omegastripes注释检查NodeList.length属性是正确的第一步。然后我想您会发现,您要么拼错了名称,要么文档中没有具有该名称的元素。

最新更新