Manually installing an add-in is relatively simple:
From the Menu, select "Tools", "Add-ins..."
Click "Browse" and navigate to the folder containing the add-in file.
Select the add-in and click OK twice.
To ease this process for the user, setting up an add-in can also be done
using code. Listing 9 shows how this can be done.
Listing 9
OptionExplicit Dim vReply AsVariant Dim AddInLibPath AsString Dim CurAddInPath AsString Const sAppName AsString = "Name Manager" Const sFilename AsString = sAppName & ".xla" Const sRegKey AsString = "FXLNameMgr" ''' RegKey for settings
Sub Setup() vReply = MsgBox("This will install " & sAppName & vbNewLine & _ "in your default Add-in directory." & vbNewLine & vbNewLine & _ "Proceed?", vbYesNo, sAppName & " Setup") If vReply = vbYes Then OnErrorResumeNext Workbooks(sFilename).Close False If Application.OperatingSystem Like "*Win*" Then CurAddInPath = ThisWorkbook.Path & "\" & sFilename AddInLibPath = Application.LibraryPath & "\" & sFilename 'User
librarypath does not have a trailing path separator 'AddInLibPath
= Application.UserLibraryPath & sFilename Else 'MAC syntax differs from Win CurAddInPath = ThisWorkbook.Path & ":" & sFilename AddInLibPath = Application.LibraryPath & sFilename EndIf OnErrorResumeNext FileCopy CurAddInPath, AddInLibPath If Err.Number <> 0 Then SomeThingWrong ExitSub EndIf With AddIns.Add(FileName:=AddInLibPath) .Installed = True EndWith Else vReply = MsgBox(prompt:="Install Cancelled", Buttons:=vbOKOnly, Title:=sAppName & " Setup") EndIf EndSub
Sub SomeThingWrong() If Application.OperatingSystem Like "*Win*" Then vReply = MsgBox(prompt:="Something went wrong during copying" _ & vbNewLine & "of the add-in to your add-in directory:" _ & vbNewLine & vbNewLine & Application.LibraryPath & "\" _ & vbNewLine & vbNewLine & "You can install " & sAppName _ & " manually by copying the file" & vbNewLine & _ sFilename & " to this directory yourself and installing the addin" _ & vbNewLine & "using Tools, Addins from the menu of Excel." _ & vbNewLine & vbNewLine & "Don't press OK yet, first do" _ & " the copying from Windows Explorer." & vbNewLine _ & "It gives you the opportunity to ALT-TAB back to Excel" _ & vbNewLine & "to read this text." _ , Buttons:=vbOKOnly, Title:=sAppName & " Setup") Else vReply = MsgBox(prompt:="Something went wrong during copying" _ & vbNewLine & "of the add-in to your add-in directory:" _ & vbNewLine & vbNewLine & Application.LibraryPath _ & vbNewLine & vbNewLine & "You can install " & sAppName & _ " manually by copying the file" & vbNewLine & sFilename & _ " to this directory yourself and installing the addin" _ & vbNewLine & "using Tools, Addins from the menu of Excel." _ & vbNewLine & vbNewLine & "Don't press OK yet," _ & " first do the copying in the Finder." _ & vbNewLine & "It gives you the opportunity to Command-TAB back to Excel" _ & vbNewLine & "to read this text." _ , Buttons:=vbOKOnly, Title:=sAppName & " Setup") EndIf EndSub
Sub Uninstall() vReply = MsgBox("This will remove the " & sAppName & vbNewLine & _ "from your system." & vbNewLine & _ vbNewLine & "Proceed?", vbYesNo, sAppName & " Setup") If vReply = vbYes Then If Application.OperatingSystem Like "*Win*" Then CurAddInPath = ThisWorkbook.Path & "\" & sFilename AddInLibPath = Application.LibraryPath & "\" & sFilename Else 'MAC syntax differs from Win CurAddInPath = ThisWorkbook.Path & ":" & sFilename AddInLibPath = Application.LibraryPath & sFilename EndIf OnErrorResumeNext Workbooks(sFilename).Close False Kill AddInLibPath DeleteSetting sRegKey MsgBox " The " & sAppName & " has been removed from your computer." _ & vbNewLine & "To complete the removal, please select the " & sAppName _ & vbNewLine & "in the following dialog and acknowledge the removal" _ , vbInformation + vbOKOnly Application.CommandBars(1).FindControl(ID:=943, recursive:=True).Execute EndIf EndSub
The setup code does the following:
Close the add-in (it might already be installed and maybe the user is
installing a new version)
Set the path where the add-in file is currently located (same path as the
path where the setup file resides)
Determine where Excel will keep it's own add-ins
Copy the add-in file from the first path to the second
Check if any errors have occurred
Include the add-in file in the list if add-ins
Install the add-in.
The code shown above can easily be adapted to use for setting up your own
addins, simply change the value of three constants: (sAppName, sFilename and sRegKey).
Also, the subroutine called "UnInstall" may be used to remove the add-in.