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

前端 未结 3 1023
离开以前
离开以前 2020-12-01 21:27

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

Set xmlDocument = New MSXML2.DOMDocument

or like this:

<         


        
3条回答
  •  一整个雨季
    2020-12-01 21:55

    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.

提交回复
热议问题