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

Question!

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

?



Answers

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.

By : JimmyPena


If you want duration per page, I'd recommand Google Analytics.

If you want a summary of database hits (ie, you run three procedures during one page load so you want to show a count of three) then I would recommend adding auditing code to your sprocs.

Alternately (though more expensively in terms of processing) you could turn on either SQL Profiler or SQL Trace and then track the database hits that way to perform statistical analysis on them.

By : Josef


I know I'm adding my answer months after this question was asked, but I thought I'd point out a decent, open-source profiler: Very Sleepy.

It doesn't have the feature count that some of the other profilers mentioned before do, but it's a pretty respectable sampling profiler that will work very well in most situations.

By : Naaff


This video can help you solving your question :)
By: admin