tags:

views:

37

answers:

3

I have an xla file that refers to some other Excel add-ins that may or may not be present on the target machine. Currently Excel fails on loading the xla with a "Compile error: Can't find project or library".

Code is something like:

  Dim result as Integer
  result = SomeAddIn.SomeFunction(x)

Prefixing this with an "On Error Goto AddInMissing" doesn't help as this is seen as a compile error.

So, is there any way of late-binding/referencing an add-in by name in VBA so that I can fail gracefully (disable certain features) if a certain add-in is not present?

+1  A: 

You can examine the Addins Collection;

if AddIns("The addins title").Installed = True then ...

or you can examine file names

For Each Item In AddIns
   Debug.? Item.Path, Item.Name
Next
Alex K.
+1 but writing `If x = True` is just horribly, horribly wrong.
Konrad Rudolph
Thanks, that has revealed something although unfortunately not quite what I'm after. (Please excuse my VBA nube-ness) Enumerating the AddIns collection lists the Add-ins as shown by the "Tools|Add-Ins..." menu in Excel. This differs from the list of VBA projects you see in Excel's VB envionment. The particular xla that I'm interested in is listed as a Project here and isn't in the AddIns collection. Is there a way of enumerating these too? (it seems that the workbooks collection doesn't appear to provide this - the first thing I tried)
John Pickup
May need to mess with Macro Security settings For Each proj In Application.VBE.VBProjects ..Debug.Print proj.Name .. Next
Alex K.
A: 

Thanks everyone. Iterating through Application.VBE.VBProjects allows me to detect if the xla is present. The next step was to wrap all calls to the functions defined in the target xla in a wrapper module. This was necessary as I'm trying to avoid getting compile errors. I only call these wrapper functions if the xla is available and fortunately VBA doesn't compile modules until they are needed (this basically meant creating two wrappers - the outer wrapper checking if the xla was available and the inner one calling the target xla).

A more sophisticated solution would probably look at both AddIns and VBE.VBEProjects

John Pickup
A: 

You don't need to use VBE.VBProjects or Addins: an XLA is a hidden member of the Workbooks collection so if you know the name of the XLA you can directly check in the Workbooks collection to see if it is open. This way you do not need the user system to have Trust Access to VBE

Charles Williams