The variable is declared as a generic Object data type.
The object which a property or method belongs to can only be resolved at run-time and not at compile time therefore running slower than early binding.
This allows for any object to be instantiated at run-time, giving great flexibility.
Dim wdObject As Object
Set wdApp = CreateObject("Word.Application")
Using late binding is not very efficient and makes programs run a lot slower.
Early-bound objects are significantly faster than late-bound objects and make your code easier to read.
Early-bound objects also make your code easier to maintain by stating exactly what kind of objects are being used.
When should it be used ?
Using late binding should be avoided if at all possible however there can be situations when it can be useful.
One example is when you do not want to include a reference to an additional type library in your VB Project.
Lets assume you are writing an Excel add-in that will be deployed to 500 users inside a large, multi-national company.
Your add-in needs to connect to a database and to do this you are using the ActiveX Data Objects library
Now this company is meant to have a Core Build and every machine is meant to have this type library (msado28.tlb) installed.
If you are 100% certain that this library is on every machine, you could use Early Binding.
Public dbADOConnect As ADODB.Connection
In this scenario the specific version of the library must be installed.
In this scenario if a machine does not have the library installed, you will see a "Missing Reference" pop up message.
In the real world though, there may be a few machines that for some reason, do not have this type library installed.
If you want to avoid a "Missing Reference" pop message you could use Late Binding.
Public dbADOConnect As Object
In this scenario the most recent version of this library that is installed will be used.
In this scenario if a machine does not have the library installed, you can check first and handle this error accordingly.
At this point you could display a nice message to the user saying:
"This machine has some missing libraries, contact your support desk. The file you are missing is blah blah".
Test and Debug with Early Binding First
If you are considering using Late Binding, it is always a good idea to use Early Binding while you are testing and debugging your code.
And then at the last minute, just before deployment you can switch the assignment to Late Binding to remove the project reference.
Another library that is often used with Late Binding is the File System Object library.
link - docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/early-late-binding
link - docs.microsoft.com/en-gb/previous-versions/office/troubleshoot/office-developer/binding-type-available-to-automation-clients
link - stackoverflow.com/questions/47056390/how-to-do-late-binding-in-vba
link - stackoverflow.com/questions/3233203/how-do-i-use-filesystemobject-in-vba
link - msofficefun.wordpress.com/2011/02/26/early-binding-vs-late-binding-in-office-vba
© 2021 Better Solutions Limited. All Rights Reserved. © 2021 Better Solutions Limited TopPrevNext