11

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")

?

shruti1810
  • 3,920
  • 2
  • 16
  • 28
Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
  • Also see [answer to the early/late binding question](http://stackoverflow.com/questions/10580/what-are-early-and-late-binding#10581). – Matthew Murdoch Oct 04 '08 at 10:13

3 Answers3

12

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.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
5

You should always use

Set xmlDocument = CreateObject("MSXML2.DOMDocument")

This is irrelevant to the binding issue. Only the declaration determines the binding.

Using CreateObject exclusively will make it easier to switch between early and late binding, since you only have to change the declaration line.

In other words, if you write this:

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

Then, to switch to late binding, you only have to change the first line (to As Object).

If you write it like this:

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

then when you switch to late binding, you have to change both lines.

JimmyPena
  • 8,694
  • 6
  • 43
  • 64
2

For the former you need to have a reference to the type library in your application. It will typically use early binding (assuming you declare your variable as MSXML2.DOMDocument rather than as Object, which you probably will), so will generally be faster and will give you intellisense support.

The latter can be used to create an instance of an object using its ProgId without needing the type library. Typically you will be using late binding.

Normally it's better to use "As New" if you have a type library, and benefit from early binding.

Joe
  • 122,218
  • 32
  • 205
  • 338
  • Joe mentioned an important VB/VBA feature feature that I wish to highlight. It's absence may be a source of confusion for less experienced VB/VBA programmers: Intellisense. FYI- that's the dropdown list that assists with auto-completion of code as it's typed. There is a related feature that has its roots in the same system: Object Explorer. FYI- that's on the view menu and in VBA is accessed by the F2 keystroke. Without early binding (using the 'New' keyword), neither Intellisense nor the Object Explorer will work. – spinjector Jul 24 '17 at 13:26