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

1

6

I'm having trouble declaring new object using 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

I don't understand why "scripting" works and "MSXML2" does not.

I'm using MS Access 2010 32 bit on Windows 7 64 bit.

Combo answered 5/5, 2016 at 9:0 Comment(0)
R
15

You don't always use the same name when using late binding. ActiveX objects require the OLE Programmatic Identifier to be used.

Change it to this:

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

From the MSDN article 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.


Razo answered 5/5, 2016 at 9:4 Comment(4)
It should be mentioned though that you should always use early binding when possible: Set x = New MSXML2.DOMDocument60 -- This way potential errors will occur at compile time not at runtime. For this to work you have to add a reference (Tools --> References) to Microsoft XML, v6.0Engrossment
I did a bit or research based on MacroMan's answer and I found a great article further explaining my question. msdn.microsoft.com/en-us/library/ms753804%28v=vs.85%29.aspxCombo
@Combo Good find - I've updated my answer to include the information from that article.Razo
@Engrossment in most cases (like this one) that is true - however there is a valid reason for using late binding, such as writing code for another office application when you don't know what version the end user will be using. This caters for any installed version rather than a reference being set to a specific version.Razo

© 2022 - 2024 — McMap. All rights reserved.