Unable to return value from VBA function

I am trying to return a value from my code. It’s much easier to just show the code:

Function writeHeaderData() As IXMLDOMNode

    Dim xmlDoc As New MSXML2.DOMDocument30
    xmlDoc.async = False
    xmlDoc.LoadXML "<Foo></Foo>"
    Dim Foo As IXMLDOMNode
    Set Foo = xmlDoc.DocumentElement

    'code snip; includes appending lots of things to Foo

    'the error is on this line:
    writeHeaderData = Foo
    Exit Function

End Function

I already searched Google, but it was useless. This function is called from the main routine, and I'm trying to add the returned IXMLDOMNode to a larger one, but I keep getting an error "Object variable or With block variable not setin the line writeHeaderData = Foo. What is here?

+3
source share
1 answer

In VB (A), if you want to assign an object variable , including assigning a return value to a function, you need to use Set, therefore:

'the error is on this line:
writeHeaderData = Foo

it should be

Set writeHeaderData = Foo
+7
source

All Articles