Difference between CreateObject and New when opening a new excel application

382 views Asked by At

What is the difference between the following two statements? I was writing a basic VB.net code which creates a new excel workbook and adds a new sheet. Both seem to be doing the same thing:

Dim oxl As Excel.Application
oxl = New Excel.Application
Dim oxl As Excel.Application
oxl = CreateObject("Excel.Application")

The only thing that I note is that the VB.net editor displays the following message when I am using NEW: "Object initialization can be simplified"

Can anyone pls help?

1

There are 1 answers

2
Eugene Astafiev On

In the sample code posted the early (or static) binding refers to compile time binding (you must add a COM reference for that):

Dim oxl As Excel.Application
oxl = New Excel.Application

And late (or dynamic) binding refers to runtime binding (for example when you use reflection) and doesn't require any COM references.

Dim oxl As Excel.Application
oxl = CreateObject("Excel.Application")

You can read more about early and late binding technologies in the Using early binding and late binding in Automation article.