我编写的以下功能无法正常工作。我在要返回对象的块的最后一行出现臭名昭著的“91”对象变量或 With block variable not set 错误。我不明白为什么,因为我的输入节点确实有具有所需 id 的子节点。MsgBox 告诉我实际上已经找到了节点,因此设置了变量 res。我仍然收到“91”错误。
Private Function getElementById(root As MSXML2.IXMLDOMNode, id As String) As MSXML2.IXMLDOMNode
Dim Children As MSXML2.IXMLDOMNodeList
Dim Child As MSXML2.IXMLDOMNode
Dim res As MSXML2.IXMLDOMNode
Dim found As Boolean
found = False
If (root.HasChildNodes = True) Then
Set Children = root.ChildNodes
For a_counter = 0 To (Children.Length - 1)
Set Child = Children.Item(a_counter)
If Child.Attributes.getNamedItem("id").Text = id Then
Set res = Child
found = True
MsgBox "Found"
End If
Next a_counter
If found = False Then
MsgBox "Not Found"
res = Nothing
End If
Else
Set res = Nothing
MsgBox "No Children"
End If
getElementById = res
End Function