VBA – Early Binding and Late Binding

Early Binding and Late Binding

When coding using VBA, as a developer, you have a choice of either using Early Binding or Late Binding.
Below I will attempt to explain in plain English what each technique consists of and go over their Pros and Cons.
Please keep in mind the advice provided below is all relative to Early and Late Binding, nothing more.

Early Binding

Early Binding consists in setting specific Library References within your VBA Project (Tools -> References).

VBA - Tools -> References Menu

VBA - References Dialog

Benefits

The benefits of using Early Binding include:

  • The use of Intellisense
    • Say your add a reference to Excel, then you gain all the Intellisense relating to Excel (properties, Methods, …)
  • The use of constants
    • Say your add a reference to Excel, then you gain the ability to directly use Excel’s application specific constants, things like: xlSolid, xlContinuous, xlCenter, xlByRows, …
  • Use of the Object Browser
  • Help (using F1)
    • You can use F1 to quickly access help relating to any property, method, …, but this will err with Late Binding.

Drawbacks

The MAJOR (or not depending on your user’s setup) drawback to Early Binding is that it can lead to versioning issues when your program is used on multiple versions of MS Office (or whatever program you are automating). Say you develop a 2013 accdb database and set the Microsoft Excel 15.0 Object Library because you perform some Excel automation. Now if you have a user try and open the accdb in 2010, they will be presented with errors at the startup
[Pic] , and the database itself will throws seemingly random errors with code that has nothing to do with Excel to the point of being unusable.
[Pic]?

On the other hand, if a user opens the same accdb in Access 2016, Access, if a newer library is registered, will automatically upgrade the reference and as such, the database will work seamlessly.

So keeping this in mind, this is why you should always perform any development using the oldest version of the program on which your solution will be run. So if you have users running Office 2013, 2016 and 2003, you need to perform your development using Office 2003 if you are using Early Binding!

Early Binding Example

'Function to open a specified Word file
'Req'd: Reference to Microsoft Word XX.X Object Library
Function OpenWordDoc1(sFileName As String)
    Dim oApp                  As Word.Application
    Dim oDoc                  As Word.Document

    On Error Resume Next
    Set oApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then    'Word isn't running so start it
        Set oApp = CreateObject("Word.Application")
    End If
    On Error GoTo Error_Handler

    Set oDoc = oApp.Documents.Open(sFileName)
    oApp.Visible = True

Error_Handler_Exit:
    On Error Resume Next
    Set oDoc = Nothing
    Set oApp = Nothing
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred." & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: OpenWordDoc" & vbCrLf & _
           "Error Description: " & Err.Description, _
           vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

The above can also be re-written as follows when using Early Binding (although I always use the above approach as there have been Bugs noted when using the New … coding)

'Function to open a specified Word file
'Req'd: Reference to Microsoft Word XX.X Object Library
Function OpenWordDoc2(sFileName As String)
    Dim oApp                  As Word.Application
    Dim oDoc                  As Word.Document

    On Error Resume Next
    Set oApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then    'Word isn't running so start it
        Set oApp = New Word.Application 'CreateObject("Word.Application")
    End If
    On Error GoTo Error_Handler

    Set oDoc = oApp.Documents.Open(sFileName)
    oApp.Visible = True

Error_Handler_Exit:
    On Error Resume Next
    Set oDoc = Nothing
    Set oApp = Nothing
    Exit Function

Error_Handler:
    MsgBox "The following error has occurred." & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: OpenWordDoc" & vbCrLf & _
           "Error Description: " & Err.Description, _
           vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

Late Binding (Dynamic Binding)

Late Binding on the other hand does not make use of pre-defined Libraries and hence it’s MAJOR benefit and thus does not suffer from versioning issues. Code written in Office 2016 will typically run just fine in Office 2013, 2010, …, 97 (always assuming the library is registered on the PC – You can’t perform say Excel automation if Excel isn’t installed!).

Late binding, or dynamic binding, is a computer programming mechanism in which the method being called upon an object is looked up by name at runtime.

Benefits

The key benefits of Late Binding include:

  • Does not require declaring Reference Libraries
  • Does not suffer from versioning issues

Drawbacks

The main drawbacks of Late Binding include:

  • Loss of Intellisense during development
  • No constants, so you have to declare the constants yourself when writing your code
  • Since the process is all done in real-time (dynamically binding to libraries in real-time), technically speaking, Late Binding will be slower that Early Binding. That said, with today’s computer hardware, the difference is truly not noticeable. So to me, this point is moot.

Late Binding Example

'Function to open a specified Word file
'Req'd: None
Function OpenWordDoc(sFileName As String)
Dim oApp As Object 'Word.Application
Dim oDoc As Object 'Word.Document
 
On Error Resume Next
    Set oApp = GetObject(, "Word.Application")
    If Err.Number <> 0 Then 'Word isn't running so start it
        Set oApp = CreateObject("Word.Application")
    End If
On Error GoTo Error_Handler
 
    Set oDoc = oApp.Documents.Open(sFileName)
    oApp.Visible = True
 
Error_Handler_Exit:
    On Error Resume Next
    Set oDoc = Nothing
    Set oApp = Nothing
    Exit Function
 
Error_Handler:
    MsgBox "The following error has occurred." & vbCrLf & vbCrLf & _
            "Error Number: " & Err.Number & vbCrLf & _
            "Error Source: OpenWordDoc" & vbCrLf & _
            "Error Description: " & Err.Description, _
            vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Function

The Best of Both World

Now that you understand the basic concepts and see the benefit of Early Binding (Intellisense to aid in coding) and the benefit of Late Binding (no versioning issues, thus can be distributed with much greater ease) we can explorer how we can combine both techniques to gain the best of both worlds.

Although the coding examples provided above are very simple ones, they do illustrate just how simple it is, normally, to convert between Early and Late Binding. As such, it is very easy during development to work in Early Binding to ease the coding process and then when you are ready to distribute your solution into production you simply switch to Late Binding by changing a few declarations and hard coding a few constants.

You may also wish to review Late Binding in Microsoft Access in which Tony Toews demonstrates how you can use conditional compiling instruction to accommodate both techniques within the same code

Some thoughts about all of the above

As an experienced developer I have, over time, come to truly value Late Binding for the flexibility it provides and use it in all my more recent code.

That being said, if you are a corporate developer in an organization with a very controlled environment in which you can be guaranteed that everyone is running the same versions of Office (or whatever program you are automating) then you can utilize Early Binding without any issues.

The Story Continues

Be sure to check out part 2 of this article: VBA – Early Binding and Late Binding – Part 2 in which I delve more into how to implement The Best of Both Worlds.

Resources

Using early binding and late binding in Automation
Early vs. Late Binding
Early vs. Late Binding
VBA references and early binding vs late binding

15 responses on “VBA – Early Binding and Late Binding

  1. Naveen Srivastava

    Before reading this article I had gone through multiple articles on the same subject but could not understand them completely however now I feel I have the clear idea about Early and Late binding in Excel VBA.
    Enjoyed your article very much as you made the topic easy to understand by writing in lea man language.
    Looking forward to getting you thoughts on some other relevant subject.

  2. Jim Fitch

    Thanks for a clear & straightforward explanation of this critical concept. I have developed several (small) applications in various versions of Access for our company that I recently began to deploy using Runtime Access. Some of the code does not execute in the Runtime version, and I eventually figured out that my problem is that all of my code is early bound and the required reference libraries are not installed on the users’ workstations. I’m now trying to figure out the best remedy(ies). Your article has helped immeasurably. Thanks.

  3. Dave Benson

    I believe I got a handle of the Early vs. Late Binding thanks to articles like this. One thing I was curious about though is some of the folks I work with write to Access from Excel using VBA. We use late binding and do not have to set the References. However, we were wondering what this would be considered (ADO, DAO, etc.) using this method.

    1. Daniel Pineault Post author

      Both are possible so it depends entirely on your code itself. Typically, unless there is a good reason to do otherwise, it is best to implement native DAO as it is typically simpler.

  4. Erik Boot

    I’m struggling with an embedded Excel (in Word without any link to another Excel file) suffering from version issues (Office 10 versus 16). I don’t see how this can be solved with late binding, any suggestion would be appreciated.

    1. Daniel Pineault Post author

      I’d need more information to be in a position to offer any suggestions. My best advice would be to post your question in the UtterAccess.com Excel forum. There are a lot of people there, including myself, that will be able to assist you.

  5. Bill J

    Just a big thank you. This is completely understandable. I GET IT. Before I knew the terms, but couldn’t tell you why I would use one over the other. I will be sure to check out your Part Deux on the “Best of Both Worlds”

  6. Michelle Wang

    I got a tool built in Access that was developed on 2013 and it stop working on 2016 version. Any suggestions ?

    1. Daniel Pineault Post author

      Michelle,

      Can you not contact the original developer for assistance?
      If not, then you have a couple choice: try and fix it yourself or hire a professional developer.

      If you want to try and resolve the issue yourself, then I’d recommend posting your question, with as much detail as possible, in an Access forum. I highly recommend utteraccess.com

      If you are looking for professional assistance, I am always available feel free to contact me through the contact page.

  7. Ian Woodward

    Thanks! And thumbs up for the experienced-based advice. It sometimes differs from what I read elsewhere, but you make it clear why.

  8. James Bear

    Great article! Flash comparisons of your code give me a strong hint about where part two is going – I’m headed there next for sure.

    I have an example of code that uses “new” at the dimensioning of the network object, as in:
    Dim nwoNew as new wshNetwork

    No “set” is used in the same code, and the routine runs (in my case) with no problem. Other than muddying the dim section at the top of code with a line that also apparently “sets”, is there a downside to doing this?

    The same code example does not set nwoNew = Nothing when it’s done doing its thing, either. Is there anything in this use of Dim that negates that bit of housekeeping?

    1. Daniel Pineault Post author

      Dim nwoNew as new wshNetwork

      Is the same as doing

      Dim nwoNew As WshNetwork
      Set nwoNew = New WshNetwork

      Or

      Dim nwoNew as Object ‘or whatever the type is
      Set nwoNew = CreateObject(“WScript.Network”)

      It all amount to the same thing. I prefer the more explicit format as for one thing it reminds me since I set it, that it should be Set xxx = Nothing at the end of my code.

      As for Setting things to Nothing. Strictly speaking VBA is supposed to perform this cleanup automatically, but there was a bug long ago where the cleanup wasn’t working and then it caused all sorts of issues. By ensuring the cleanup yourself, you ensure you never have any issues, ever!