top of page
  • John

Add Enum from Type Library

If you ever write code using early binding but then convert it to use late binding before release, then you'll know the frustration of starting out using Enum member's names only having to convert them to values when you move to late binding (or using the values from the outset and then not remember what the values mean).


Then, later, when you have to edit or update your code, you're left with a load of 'magic numbers' (with code comments explaining what those numbers mean ... if you had the time to make those comments ... or you have to find the relevant web page with the explanation of all the numbers, if you can find it).


VBE_Extras provides a solution ... use the Enum member's names during early binding and then keep them for late binding!


How? By adding the Enum from the Type Library into your VBA code. In the VBE, select 'Extras' > 'Other tools' > 'Add Enum from Type Library'. Then select the Type Library e.g. 'Scripting' and then select the Enum e.g. 'FileAttribute' and this ...



... will be added to your VBA code. Or perhaps you are automating Outlook from another host application (Excel, Access etc) and you struggle to remember which OlItemType refers to which type of Item? Go to 'Add Enum from Type Library' then select the 'Outlook' Type Library then select the 'OlItemType' Enum and this ...



... will be added to your VBA code.


Now you can keep your Enum member names when you switch to late binding ... no more 'magic values'.


Note that adding an Enum via the 'Add Enum from Type Library' function works if you are using special binding (i.e. if you have the <Ref> code comment in place in the relevant Module then you don't need to add a Project Reference to the Type Library beforehand).

Comments


bottom of page