What are the differences between using the New keyword and calling CreateObject in Excel VBA?

calendar_today Asked Oct 4, 2008
thumb_up 7 upvotes
history Updated April 16, 2026

Question posted 2008 · +6 upvotes

What criteria should I use to decide whether I write VBA code like this:

Set xmlDocument = New MSXML2.DOMDocument

or like this:

Set xmlDocument = CreateObject("MSXML2.DOMDocument")

?

Accepted answer +7 upvotes

As long as the variable is not typed as object

Dim xmlDocument as MSXML2.DOMDocument
Set xmlDocument = CreateObject("MSXML2.DOMDocument")

is the same as

Dim xmlDocument as MSXML2.DOMDocument
Set xmlDocument = New MSXML2.DOMDocument

both use early binding. Whereas

Dim xmlDocument as Object
Set xmlDocument = CreateObject("MSXML2.DOMDocument")

uses late binding. See MSDN here.

When you’re creating externally provided objects, there are no differences between the New operator, declaring a variable As New, and using the CreateObject function.

New requires that a type library is referenced. Whereas CreateObject uses the registry.

CreateObject can be used to create an object on a remote machine.

3 code variants in this answer

  • Variant 1 — 2 lines, starts with Dim xmlDocument as MSXML2.DOMDocument
  • Variant 2 — 2 lines, starts with Dim xmlDocument as MSXML2.DOMDocument
  • Variant 3 — 2 lines, starts with Dim xmlDocument as Object

Top excel-vba Q&A (6)

+7 upvotes ranks this answer #101 out of 136 excel-vba solutions on this site .