Automatically install Excel VBA add-on

I wrote an excel VBA extension file (.xlam). I also have an exported ribbon setup (.exportedUI). How do I create an installer so that my users can just run the installer to install the excel VBA add-in and customize the ribbon?

+3


source to share


2 answers


If you want to use a commercial tool, Advanced Installer has built-in support for Office add-on installers: http://www.advancedinstaller.com/user-guide/addins-tab.html



Another approach is to use custom code to customize the add-in. Maybe this will help: http://www.cpearson.com/excel/installinganxla.aspx

+4


source


I have created an unattended installation routine that will be added to the "This Workbook" section of the XLAM file so that it will automatically start when the file is opened. To distinguish between the installation file and the installed file, the installation version is named ".install.xlam" and the installed version is named ".xlam". (Otherwise Excel has "Sorry, Excel cannot open two workbooks with the same name at the same time."

Procedure: - Rename your XLAM file with .install.xlam - Open it and edit it in Visual Basic Editor (VBE) - Add the following procedures to the "This Workbook" section in VBE - Save the file

To split / install XLAM, now you just need to ask the user to double-click the XLAM file, enable macros as needed, and accept the add-in installation.



If you want to update your XLAM later, you just double-click it, enable macros as needed and refuse to install it. Then edit it and save your changes.

Here's the code to add to "ThisWorkbook":

β€˜ (c) Willy Roche (willy.roche(at)centraliens.net)
β€˜ Install procedure of XLAM (library of functions)
β€˜ This procedure will install a file name .install.xlam in the proper excel directory
β€˜ The install package will be name
β€˜ During install you may be prompt to enable macros (accept it)
β€˜ You can accept to install or refuse (which let you modify the XLAM file macros or install procedure

Option Explicit
Const bVerboseMessages = False β€˜ Set it to True to be able to Debug install mechanism
Dim bAlreadyRun As Boolean β€˜ Will be use to verify if the procedure has already been run

Private Sub Workbook_Open()
β€˜ This sub will automatically start when xlam file is opened (both install version and installed version)
Dim oAddIn As Object, oXLApp As Object, oWorkbook As Workbook
Dim i As Integer
Dim iAddIn As Integer
Dim bAlreadyInstalled As Boolean
Dim sAddInName As String, sAddInFileName As String, sCurrentPath As String, sStandardPath As String

sCurrentPath = Me.Path & "\"
sStandardPath = Application.UserLibraryPath β€˜ Should be Environ("AppData") & "\Microsoft\AddIns"
DebugBox ("Called from:'" & sCurrentPath & "β€˜")

If InStr(1, Me.Name, ".install.xlam", vbTextCompare) Then
β€˜ This is an install version, so let’s pick the proper AddIn name
sAddInName = Left(Me.Name, InStr(1, Me.Name, ".install.xlam", vbTextCompare) – 1)
sAddInFileName = sAddInName & ".xlam"

β€˜ Avoid the re-entry of script after activating the addin
If Not (bAlreadyRun) Then
DebugBox ("Called from:'" & sCurrentPath & "β€˜ bAlreadyRun = false")
bAlreadyRun = True β€˜ Ensure we won’t install it multiple times (because Excel reopen files after an XLAM installation)
If MsgBox("Do you want to install/overwrite β€˜" & sAddInName & "β€˜ AddIn ?", vbYesNo) = vbYes Then
β€˜ Create a workbook otherwise, we get into troubles as Application.AddIns may not exist
Set oXLApp = Application
Set oWorkbook = oXLApp.Workbooks.Add
β€˜ Test if AddIn already installed
For i = 1 To Me.Application.AddIns.Count
If Me.Application.AddIns.Item(i).FullName = sStandardPath & sAddInFileName Then
bAlreadyInstalled = True
iAddIn = i
End If
Next i
If bAlreadyInstalled Then
β€˜ Already installed
DebugBox ("Called from:'" & sCurrentPath & "β€˜ Already installed")
If Me.Application.AddIns.Item(iAddIn).Installed Then
β€˜ Deactivate the add-in to be able to overwrite the file
Me.Application.AddIns.Item(iAddIn).Installed = False
Me.SaveCopyAs sStandardPath & sAddInFileName
Me.Application.AddIns.Item(iAddIn).Installed = True
MsgBox ("β€˜" & sAddInName & "β€˜ AddIn Overwritten")
Else
Me.SaveCopyAs sStandardPath & sAddInFileName
Me.Application.AddIns.Item(iAddIn).Installed = True
MsgBox ("β€˜" & sAddInName & "β€˜ AddIn Overwritten & Reactivated")
End If
Else
β€˜ Not yet installed
DebugBox ("Called from:'" & sCurrentPath & "β€˜ Not installed")
Me.SaveCopyAs sStandardPath & sAddInFileName
Set oAddIn = oXLApp.AddIns.Add(sStandardPath & sAddInFileName, True)
oAddIn.Installed = True
MsgBox ("β€˜" & sAddInName & "β€˜ AddIn Installed and Activated")
End If
oWorkbook.Close (False) β€˜ Close the workbook opened by the install script
oXLApp.Quit β€˜ Close the app opened by the install script
Set oWorkbook = Nothing β€˜ Free memory
Set oXLApp = Nothing β€˜ Free memory
Me.Close (False)
End If
Else
DebugBox ("Called from:'" & sCurrentPath & "β€˜ Already Run")
β€˜ Already run, so nothing to do
End If
Else
DebugBox ("Called from:'" & sCurrentPath & "β€˜ in place")
β€˜ Already in right place, so nothing to do
End If
End Sub

Sub DebugBox(sText As String)
If bVerboseMessages Then MsgBox (sText)
End Sub

      

+2


source







All Articles