我正在尝试运行以下用户定义的函数,但是我收到以下错误:
对象变量或未设置块变量
Private Function Find_Select_Option(selectElement As HTMLSelectElement, optionText As String) As Integer
Dim i As Integer
Find_Select_Option = -1
i = 0
While i < selectElement.Options.length And Find_Select_Option = -1 ' ### error occurs on this line
DoEvents
If LCase(Trim(selectElement.Item(i).Text)) = LCase(Trim(optionText)) Then Find_Select_Option = i
i = i + 1
Wend
End Function
我已在下面添加了VBA代码(source)。请仔细阅读并告诉我这段代码有什么问题。
Public Sub IE1()
Dim URL As String
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
URL = "http://douglasne.mapping-online.com/DouglasCoNe/static/valuation.jsp"
Set IE = New InternetExplorer
With IE
.Visible = True
.navigate URL
While .Busy Or .readyState <> READYSTATE_COMPLETE
DoEvents
Wend
Set HTMLdoc = .document
End With
'<select name="StreetDir">
Dim optionIndex As Integer
Dim dirSelect As HTMLSelectElement
Set dirSelect = HTMLdoc.getElementsByName("StreetDir")(0)
'dirSelect.selectedIndex = 2 'set option index directly
optionIndex = Find_Select_Option(dirSelect, "E")
If optionIndex >= 0 Then
dirSelect.selectedIndex = optionIndex
End If
'<select name="StreetSfx">
Dim suffixSelect As HTMLSelectElement
Set suffixSelect = HTMLdoc.getElementsByName("StreetSfx")(0)
optionIndex = Find_Select_Option(suffixSelect, "PLAZA")
If optionIndex >= 0 Then
suffixSelect.selectedIndex = optionIndex
End If
End Sub
我该如何解决这个问题?
答案 0 :(得分:0)
当我四处寻找时,我也看到了the OzGrid post you're pulling from。问题是测试网址http://douglasne.mapping-online.com/DouglasCoNe/static/valuation.jsp不再具有您要查找的元素!例如,它没有<select name="StreetDir">
。因此,当您致电dirSelect
时,Nothing
为Find_Select_Option
。
我建议使用本地文件进行测试。例如,使用以下内容创建c:\users\prashant\foo.htm
(或您想要放置的任何位置)(从w3schools修改):
<!DOCTYPE html>
<html>
<body>
<select name="Car">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
</body>
</html>
然后下面的代码应该可以工作(它适用于我):
Public Sub IE1()
Dim URL As String
Dim IE As SHDocVw.InternetExplorer
Dim HTMLdoc As MSHTML.HTMLDocument
URL = "c:\users\prashant\foo.htm" ' *** Read from a local file
Set IE = New InternetExplorer
With IE
.Visible = True
.navigate URL
While .Busy Or .ReadyState <> READYSTATE_COMPLETE
DoEvents
Wend
Set HTMLdoc = .document
End With
'<select name="Car">
Dim optionIndex As Integer
Dim dirSelect As HTMLSelectElement
Dim message As String
Set dirSelect = Nothing ' *** Set up for the error checking below
On Error Resume Next
'Set dirSelect = HTMLdoc.getElementsByTagName("select").Item(0) ' This is OK, too
Set dirSelect = HTMLdoc.getElementsByName("Car").Item(0) ' *** It exists!
' *** Here's some error-checking code you can use
If Err.Number <> 0 Then ' Report errors
message = "Error " & CStr(Err.Number) & vbCrLf & Err.Description
ElseIf dirSelect Is Nothing Then
message = "No element found"
Else
message = "OK" & vbCrLf & dirSelect.textContent
End If
On Error GoTo 0 ' *** Back to normal
MsgBox message
End Sub
当getElementsByName
的参数为"Car"
时,我会收到OK
响应。当我将该参数更改为"Nonexistent"
时,我得到No element found
。这确认了您在调用dirSelect
时代码中Nothing
为Find_Select_Option
。