VBA Libraries


Libraries add pre-defined code structures (such as functions, subroutines among others) in a simple and easy way.

In the VBA context, a library usually allows access to new objects and functions.


Standard Libraries

Libraries that are enabled by default when opening Excel:

Standard Libraries
  1. Excel – This is a collection of classes available in Excel, allowing you to work with worksheets objects E.g. workbooks, spreadsheets, ranges, etc...
  2. Office – It is a generic class collection for all Office suite applications, allowing you to work with Office objects in general E.g. command bar, help, etc...
  3. stdole – It is the standard collection of the OLE class, it allows exchange of information between applications
  4. VBA – It is the class collection that allows functions to be used E.g. MsgBox, InputBox, string functions, etc...
  5. VBAProject – It is the local collection for the active workbook, (E.g. spreadsheets, users, etc...)

Class: is what defines the structure of a given object (E.g. for class Range we have objects like Range("A1"), Range("C2:B5"), and so on).


Enabling Libraries

To access, enable and disable libraries in VBA:

  1. In Excel, open the VBE (Alt+F11)
  2. On the Menu Bar, click Tools
  3. Select References... (the References – VBAProject dialog box will be displayed)
  4. Select the desired libraries
  5. Click OK
Enabling Libraries

To add a library that is not in the list, use the button and select the file of the new library.

Enabling additional libraries allows you to develop codes that communicate with other applications and processes. E.g. Word, Outlook, DAO (Data Access Objects), ADOdb (Active Data Objects database), etc...

When we refer to a library in the code but forget to enable it, the following error message is displayed:

Librarie Not Defined

Object Browser

The Object Browser allows you to view the list of all the different objects, methods, and properties of the enabled libraries.

To search the libraries:

  1. Click on the Object Browser icon on the toolbar ()
  2. Select the library you want to view on the dropdown menu
  3. Navigate the library
Object Browser

When in doubt, the Microsoft Help button () can elucidate the utility of the selected element.


VBA Early Binding vs Late Binding

You can use structures from a library without enabling it. This procedure is called Late Binding.

Dim OutApp As Object
Set OutApp = CreateObject("Outlook.Application")

Usually for the Late Binding a variable is declared as an object and later associated with CreateObject.

When we enable a library to access its structures we are performing what is called Early Binding.

Dim OutApp As New Outlook.Application

In the example case the Outlook library was enabled ().


Early & Late Binding Differences

Early: From the point of view of code development and performance time, Early Binding is more suitable because it allows you to use Excel intellisense ( + ) as well as the library object browser.

Late: Regarding compatibility errors, mainly due to the use of different versions of Excel (which changes the version of the libraries), Late Binding is more indicated.

Intellisense: is an aid, mainly to autocomplete the code, with useful information ( + ).

Intellisense

So, to gain practice with the libraries, try to write the code with Early Binding and later change it into Late Binding.



SuperExcelVBA.com is learning website. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. All Rights Reserved.

Excel ® is a registered trademark of the Microsoft Corporation.

© 2024 SuperExcelVBA | ABOUT

Protected by Copyscape