Saturday, February 04, 2017

VBA Excel - Using CreateObject

There are some very useful libaries that are not part of Excel VBA.
These include the Dictionary, Database objects, Outlook VBA objects, Word VBA objects and so on.

These are written using COM interfaces.
The beauty of COM is that was can easily use these libraries in our projects.

If we add a reference to the library we create the object in the normal way.

' Select Tools->References and place a check
' beside "Microsoft Scripting Runtime"
Dim dict As New Scripting.Dictionary

If we don’t use a reference we can create the object at run time using CreateObject.

Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")

The first method is referred to as Early Binding and the second is referred to as Late Binding(see Early versus Late Binding) for more details.

http://excelmacromastery.com/vba-objects/#Subtle_Differences_of_Dim_Versus_Set

No comments :