无法从VBA函数返回值

时间:2012-05-14 15:45:59

标签: excel vba excel-vba ms-office

我正在尝试从代码中返回一个值。只显示代码要容易得多:

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

我已经谷歌搜索了,但它无济于事。从main子例程调用此函数,我正在尝试将返回的IXMLDOMNode附加到更大的函数,但我在"Object variable or With block variable not set行上出现writeHeaderData = Foo“错误。这里有什么?

1 个答案:

答案 0 :(得分:7)

在VB(A)中,如果要分配对象变量,包括分配函数的返回值,则需要使用{{1} },所以:

Set

应该是

'the error is on this line:
writeHeaderData = Foo