Search code examples
excelvbalate-bindingearly-binding

Late Binding Issue with "MSXML2.XMLHTTP60"


I am getting a Run-time error '429': ActiveX component can't create object error when I try to run the following code.

Option Explicit

Private Sub EarlyVsLateBinding()

    ' References to both the Microsoft Scripting Runtime and Microsoft XML, v6.0 
    ' are active so that early binding is possible

    Dim EarlyDictionary As Scripting.Dictionary
    
    Dim LateDictionary As Object
    Set LateDictionary = CreateObject("Scripting.Dictionary")
    
    Dim EarlyHTTP As New MSXML2.XMLHTTP60
    
    Dim LateHTTP As Object
    Set LateHTTP = CreateObject("MSXML2.XMLHTTP60") ' Error is thrown here

End Sub

I have included the example of Scripting.Dictionary to convince myself that the CreateObject function wasn't causing any issues, and to show that an early and late binding work for another class.

Sadly, every example that I come across of this class uses the early binding method but I need the late binding method for this code. Also, replacing the line Set LateHTTP = CreateObject("MSXML2.XMLHTTP60") with Set LateHTTP = GetObject(Class:="MSXML2.XMLHTTP60") yielded the same error.

What could be causing this error?


Solution

  • As @Raymon Wu pointed out in the comments of the question, changing the line

    Set LateHTTP = CreateObject("MSXML2.XMLHTTP60")

    to

    Set LateHTTP = CreateObject("MSXML2.XMLHTTP")

    worked partially.


    Edit 1

    Alternatively, as @KL-1 has pointed out in the comments, changing the line to

    Set LateHTTP = CreateObject("MSXML.XMLHTTP.6.0")

    fixed the issue as well.


    This change does make the code in my question run without error. However, this caused another error later in a different piece of code

    LateHTTP.setRequestHeader bstrHeader:="Content-Type", bstrValue:="application/json"

    which was the Run-time error '448': Named argument not found error. This was fixed by removing the named arguments and changing the line to

    LateHTTP.setRequestHeader "Content-Type", "application/json"


    Edit 1

    Note that both solutions caused this Run-time error '448'.

    As others have pointed out in the comments, when binding classes, the names are not necessarily the same when using early vs late.

    The names for late binding can be found in the computer's registry editor under Computer\HKEY_CLASSES_ROOT.


    I am aware that this goes beyond the scope of the question, but I consider it relevant information.