chrisdotcode
chrisdotcode

Reputation: 1601

Can't return a value from a VBA function

I'm 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've already Google searched, but it's been of no avail. This function is being called from the main subroutine, and I'm trying to append the returned IXMLDOMNode to a bigger one, but I keep getting an "Object variable or With block variable not set" error on the writeHeaderData = Foo line. What's up here?

Upvotes: 4

Views: 5644

Answers (1)

AakashM
AakashM

Reputation: 63378

In VB(A), when you want to assign to an object variable, including assigning the return value of a function, you need to use Set, so:

'the error is on this line:
writeHeaderData = Foo

should be

Set writeHeaderData = Foo

Upvotes: 9

Related Questions