VBA CreateObject("MSXML2.DOMDocument60") 抛出错误 429

VBA CreateObject("MSXML2.DOMDocument60") throws an Error 429

我在使用 CreateObject()

声明新对象时遇到问题
Sub A()
    Dim x

    'This works
    Set x = CreateObject("Scripting.FileSystemObject")
    Set x = Nothing

    'This throws an error 429 "Active component cannot create object."
    Set x = CreateObject("MSXML2.DOMDocument60")
    Set x = Nothing

    'The only way I can create object is to add the reference using GUID
    Dim y As MSXML2.DOMDocument60
    Set y = New MSXML2.DOMDocument60
    Set y = Nothing
    'This works like a charm

End Sub

我不明白为什么 "scripting" 有效而 "MSXML2" 无效。

我在 Windows 7 64 位上使用 MS Access 2010 32 位。

您在使用后期绑定时并不总是使用相同的名称。 ActiveX 对象需要 OLE Programmatic Identifier 才能使用。

改成这样:

Set x = CreateObject("MSXML2.DOMDocument.6.0")

来自 MSDN 文章 Building MSXML Applications

When you are using a scripting language, you can identify the control via its ProgID, which is a form that is quite a bit easier to read by a human. An example of a ProgID is Msxml2.DOMDocument.6.0.