我写的以下功能无法正常工作。我得到臭名昭着的'91'对象变量或With块变量没有设置错误在我要返回对象的块的最后一行。我不明白为什么,因为我的输入节点确实具有所需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