Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 793
chrisdotcode Avatar asked Oct 31 '25 20:10

chrisdotcode


1 Answers

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
like image 174
AakashM Avatar answered Nov 02 '25 12:11

AakashM