View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
olivier durand olivier durand is offline
external usenet poster
 
Posts: 7
Default References to XLA files

Hi!

This question has been asked before and today I am facing the same
problem. Luckily by browsing this forum and using a bit of intuition I
managed to find a solution to this common problem.

If you use the code below you should be able to assign a relative path
to your xla (add-in) file. This will allow you to distribute your
excel files more easily (e.g. by providing your xls files & and
add-ins under one folder). So, here is the code (to be saved in "This
workbook"):

Private Sub Workbook_Open()

Dim response As Integer

On Error Resume Next
If ActiveWorkbook.VBProject.References("<ADDIN NAME").IsBroken _
Then
On Error GoTo Err_handle
ActiveWorkbook.VBProject.References.AddFromFile _
ActiveWorkbook.Path & "\<YOUR ADDIN PATH AND FILENAME"
End If

Exit Sub

Err_handle:
response = MsgBox("Could not Reference ""xxx.xla""" & _
vbCr & """xxx.xla"" must be under directory yyy", _
vbOKOnly + vbExclamation)

End Sub

Description:
Because the code is in "ThisWorkbook" object, it will be run whenever
the workbook is opened.

If the referenced addin is available the "if" statement will do
nothing and the sub will exit. On the other hand, if the addin is not
available, the "if" statement will generate an error. However because
the first "on error" statement is placed just before, it will try to
re-attach the addin by using the "addFromFile" method of the
References collection. In turn, if this fails the error handler will
catch it (thanks to the second "On error" statement) and displays the
error message.

Possible improvements
Include all the addins under a given folder (e.g like winamp behaves
with plugins).

Regards,

Olivier