无法从VBA函数返回值

我试图从我的代码中返回一个值。 显示代码要容易得多:

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 

我已经search了谷歌,但它是没有用的。 这个函数是从主子程序中调用的,我试图将返回的IXMLDOMNode附加到更大的一个,但是我一直在writeHeaderData = Foo行上收到"Object variable or With block variable not set ”错误。 这是什么?

在VB(A)中,当你想要分配给一个对象variables, 包括分配一个函数的返回值时,你需要使用Set ,所以:

 'the error is on this line: writeHeaderData = Foo 

应该

 Set writeHeaderData = Foo