top of page
  • John

Special binding

Updated: 3 days ago

If you are aware of the differences between early and late binding and often develop your VBA code using the former but switch to the latter when deploying, then 'special binding' may be useful to you.

 

Using VBE_Extras ability to parse Type Libraries allows for "early binding like" functionality during development while still actually using late-binding. For example, the Show All Members function of VBE_Extras can show you members of a Class in a Type Library without having a Project Reference to the Type Library and without using early binding.


Special binding

In the image above, you can see that fso is declared As Object (i.e. it is late bound) but the Show All Members dialog is still showing all of the child members of fso as though it were defined As Scripting.FileSystemObject (i.e. as though it were early bound). Three things need to be in place for this to work:


  1. You need to have enabled 'special binding' in the 'Code' tab of the VBE_Extras Settings.

  2. Following the declaration (in this case Dim fso As Object), you need to tell VBE_Extras what the early-bound equivalent type would be. This is done as a code comment on the same line as the declaration – the code comment must include the word As and then the type … so here it is As Scripting.FileSystemObject †. This needs to happen after each declaration that you want special binding to work for.

  3. At the top of the Module in which you want special binding to work, in the declaration lines (or, at least, before the first procedure or property is declared), you need to tell VBE_Extras which Type Library(s) you want special binding for by specifying the full path to the Type Library in a code comment – in this case it is the Microsoft Scripting Runtime library and so the code comment is' <Ref>C:\Windows\System32\scrrun.dll</Ref> "Scripting" … the path must be enclosed in <Ref> tags - anything after the closing </Ref> tag is ignored (but I find it useful to include the 'program name' for the Type Library as a reminder ... for the Microsoft Scripting Runtime, the program name is "Scripting"). You can type the relevant code comment out yourself but, much simpler, is to get the relevant code comment for a specific Type Library using the Update Project References dialog - just right-click on the relevant Type Library and select the 'Copy as <Ref>' menu item.


† in the example code I have used As Scripting.FileSystemObject though As FileSystemObject would do - but including Scripting is better (as it is when using early binding) to correctly qualify the type and make the process of identifying which Type Library the type is in simpler and less prone to name clashes


The VBE_Extras Functions that support special binding are:



... see the VBE_Extras user guide for more details on each of the above functions.


The 'early bound equivalent type' code comment


You must include the word As followed by the relevant type in the code comment following the declaration of the variable that you want special binding to work for.

 

For variables (including Module-level variables), Statics, Functions, Property Gets and Type elements, the code comment should be at the end of the line on which they are declared. I recommend only one declaration on any one line unless you want to use the same 'early bound equivalent type' for multiple variables e.g.



… will set the early bound equivalent type to Outlook.Folder for each of a, b and c (not just for c).

 

For parameters, the code comment should be in a line either above or below the procedure / property declaration and it should be preceded by the name of the parameter e.g.



… will set the early bound equivalent type to Scripting.FileSystemObject for fso and to Scripting.Dictionary for dict.

 

The As Type part of the code comment can be enclosed in parenthesis if desired, for example the following is also valid:



The code-comment can be on a subsequent physical line (i.e. following a line-continuation character) if required, however, everything from the apostrophe starting the code-comment to the name of the type must be on a single physical line.


Special binding – things to know


As noted above, you must include As in the code comment following the declaration of the variable that you want special binding to work for!


All Project References have a specific priority order (the order is as per the Project References in the VBE's References dialog which is the same as in VBE_Extras equivalent Update Project References dialog) such that if multiple Type Libraries (or VBA Projects … whatever the reference is to) have a member with the same name, the Project Reference with the higher priority 'wins'. The Type Library(s) "Referenced" by <Ref> code comments are considered to have a lower priority than all actual Project References and the priority order for them is determined by the order they are specified in the Module (those appearing first = highest priority). If a single Type Library is referenced via an actual Project Reference and via a <Ref> code comment then the <Ref> code comment will be ignored.

 

The path in the <Ref> code comment must be an absolute path (not a relative path).

 

Special binding works with hidden members of Type Libraries if the relevant setting (in the Object Browser, right click on any Member and select 'Show Hidden Members', or use the VBE_Extras function of the same name in the main menu) is switched on.


If you deploy your 'special binding' VBA code to other users / devices, those users / devices DO NOT need to have VBE_Extras installed. On any device that does not have VBE_Extras installed, VBA code will perform just like any other late-bound VBA code.

bottom of page