0

I am getting an error 438 - object doesn't support this property or method when findChildren() is called. It doesn't seem to have issues on how i declared or set my variables so not sure why it doesn't like the function call. It doesn't even get inside the function either before erroring out.

Also when i print the nodes base for root and child it prints just fine so i know something exists there and its successfully grabbing the node.

Option Explicit

Sub TestXML()
On Error GoTo ErrHandle
    Dim XDoc As New MSXML2.DOMDocument60
    
    Dim root As IXMLDOMNode
    
    Dim firstChild As IXMLDOMNode
    
    
    XDoc.async = False: XDoc.validateOnParse = False
    XDoc.Load (ThisWorkbook.Path & "\test.xml")
    
    'Get Document Elements
    Set root = XDoc.DocumentElement
    Set firstChild = root.firstChild
    
    Debug.Print "---Begin New---"
    
    Debug.Print root.BaseName
    Debug.Print root.Text
    
    Debug.Print firstChild.BaseName
    Debug.Print firstChild.Text
    
    findChildren (firstChild)
    
    Set XDoc = Nothing

    Exit Sub

ErrHandle:
    MsgBox Err.Number & " - " & Err.Description, vbCritical
    Exit Sub

End Sub

 
Function findChildren(x As IXMLDOMNode)

    If x.HasChildNodes() Then
        findChildren (x)
        
    ElseIf (x.NextSibling <> Null) Then
    
        findChildren (x.NextSibling)
        Debug.Print "[" & x.NextSibling.BaseName & "] = [" & x.NextSibling.Text & "]"
        
    Else
    
        Debug.Print "[" & x.BaseName & "] = [" & x.Text & "]"
    
    
    End If
    

End Function


necroncryptek
  • 235
  • 1
  • 2
  • 7
  • 1
    have you tried it without the parentheses? `findChildren firstChild` – Geert Bellekens Dec 04 '20 at 09:33
  • that is utterly insane to me that it works. I tried defining the function without ()s and it flips out, but i can't call the function with ()s? Why is it like that? – necroncryptek Dec 04 '20 at 09:44
  • 2
    You can (must) if you use the return value of the function. Now you are basically using the function as a sub, which means you shouldn't use parentheses. The problem is that the compiler only picks this up as an error if you have more than one parameters. That is because you are allowed to put each individual parameter between parentheses. This has another effect (something to do with passing parameter as reference vs value), and that is what went wrong here. I suspect it is trying to create a clone of the object, which isn't supported by the object itself. – Geert Bellekens Dec 04 '20 at 09:55
  • @necroncryptek you might have a look at [Displaying XML structures including attributes via recursive function calls](https://stackoverflow.com/questions/51887820/obtain-attribute-names-from-xml-using-vba/51919182#51919182) – T.M. Dec 04 '20 at 10:08
  • @necroncryptek - is your intention to find only childnodes of the next subordinated hierarchy level or to find all subordinated levels? – T.M. Dec 04 '20 at 20:08

0 Answers0