Home » excel » excel – Deploying Macros as Add Ins with Custom Ribbon Buttons to the entire office

excel – Deploying Macros as Add Ins with Custom Ribbon Buttons to the entire office

Posted by: admin May 14, 2020 Leave a comment


I have been searching for a way to distribute macros to my tech illiterate office in the simplest way I can.

From my research, saving the Macros into a .xlam add-in would seem to be headed in the right direction.

Is it also possible to set up a custom ribbon tab in this manner?

Thus far I have failed to find any guides and our office security may also block certain avenues.

Using W-Hit’s excellent solution, and setting up the folder structure as indicated, it definitely helps make deploying an update much easier with the DeployAddIn subroutine.

I also found it useful to put the DeployAddIn and InstallAddin subroutines into a custom ribbon tab of their own!

I ran into a problem with the InstallAddin subroutine however: how to format the XML text in VBA without running into syntax errors.

I discovered that Each Element must have mso at the start e.g. <button> becomes <mso:button> and each “speech marked section” in a line must have “”double speech marks”.

Perhaps the easiest way to use this install function is to save and edit the code into an active file, then open C:\Users[username]\AppData\Local\Microsoft\Office\Excel.officeUI in Notepad++. Then simply perform a find and replace to add in the extra quotation marks and paste it into the ribbonXML = “insert your text here” section of the code, ensuring it is encapsulated by the final speech marks to mark the entire section as a text string.

I might also look into adding extra functionality here… having an inputbox or userform that allows you to paste the code in at this point rather than have you enter the VBA editor to paste it in.

How to&Answers:

I currently do this, and it’s a somewhat in depth process to setup, but runs smoothly once it is.

1st step is to create a folder structure with testing and production copies of your .xlam files that you are the admin for.

2nd, in the production folder, right click all .xlam files and set the attributes in the properties to Read-only. If you don’t you’ll never be able to update the addin if anyone else is in it.

3rd, when you make updates to the code in the testing file, just replace the production file with the updated file, and change to Read-only again. Users will only have to close all instances of excel and reopen to have the most up-to-date copy of the add-in.

Below is an admin add-in I use to move testing files to production.

Sub DeployAddIn()
'Author       : Ken Puls (www.excelguru.ca)
'Macro Purpose: To deploy finished/updated add-in to a network
'               location as a read only file

    Dim strAddinDevelopmentPath As String
    Dim strAddinPublicPath As String
    Dim FSO As New FileSystemObject

    'Set development path
    ChDrive "R:"
    ChDir "R:\addins\PROJECTS"
    strAddinDevelopmentPath = Application.GetOpenFilename()
    If strAddinDevelopmentPath = "False" Then
        Exit Sub
    ElseIf InStr(strAddinDevelopmentPath, "\PRODUCTION\") > 1 Then
        If MsgBox("You've Selected a Production File To Replace a Production File. Would You Like To Continue Anyway?", vbYesNo) = vbNo Then
            Exit Sub
        End If
    End If

    'Get Desitination path
    strAddinPublicPath = Replace(strAddinDevelopmentPath, "TESTING", "PRODUCTION")

    'Create dir if it doesn't exist
    On Error Resume Next
    MkDir Left(strAddinPublicPath, InStrRev(strAddinPublicPath, "\") - 1)
    On Error GoTo 0

    'Turn off alert regarding overwriting existing files
    Application.DisplayAlerts = False

    'overwrite existing file
    On Error Resume Next
    SetAttr strAddinPublicPath, vbNormal
    On Error GoTo 0
    FSO.CopyFile strAddinDevelopmentPath, strAddinPublicPath, True
    SetAttr strAddinPublicPath, vbReadOnly

    'Resume alerts
    Application.DisplayAlerts = True
End Sub

4th, I’ve also written a macro to change the custom ribbon. The below link, in addition Ron deBruin’s site is useful. https://grishagin.com/vba/2017/01/11/automatic-excel-addin-installation.html
Code to automate addin install after you get the right text from the officeUI file

Sub InstallAddin()
    'Adapted from https://grishagin.com/vba/2017/01/11/automatic-excel-addin-installation.html
    Dim eai As Excel.AddIn
    Dim alreadyinstalled As Boolean
    Dim ribbonXML As String

    'check if already installed
    For Each eai In Application.AddIns
        If eai.Name = "Main addin.xlam" Then
            eai.Installed = False
            Exit For
        End If

    'add and install the addin
    Set eai = Application.AddIns.Add("path to Main addin.xlam", False)
    eai.Installed = True

    'append quick access ribbon xml to add button

    'have to close addin for it to load properly the first time
    Workbooks("Main addin.xlam").Close

End Sub

Sub ClearCustRibbon()
Dim hFile As Long
Dim ribbonXMLString As String

hFile = FreeFile
OfficeUIFilePath = Environ("USERPROFILE") & "\AppData\Local\Microsoft\Office\Excel.officeUI"

ribbonXMLString = "<mso:customUI           xmlns:mso=""http://schemas.microsoft.com/office/2009/07/customui"">" & _
"<mso:ribbon>" & _
"<mso:qat>" & _
"<mso:sharedControls>" & _
"</mso:sharedControls>" & _
"</mso:qat>" & _
"</mso:ribbon>" & _

Open OfficeUIFilePath For Output Access Write As hFile
Print #hFile, ribbonXMLString
Close hFile

End Sub

Sub LoadNewRibbon()
Dim hFile As Long

hFile = FreeFile
OfficeUIFilePath = Environ("USERPROFILE") & "\AppData\Local\Microsoft\Office\Excel.officeUI"

ribbonXML = "your ribbon text here"

Open OfficeUIFilePath For Output Access Write As hFile
Print #hFile, ribbonXML
Close hFile
End Sub

***IMPORTANT—- If you install the addin manually, make sure you select no when prompted if you want the file saved to the local machine. If you save it to the local machine, it creates a local copy, and will never update if you make changes to the network copy or need to fix an error.

There are more tips, but you’ll mostly need to adapt them to fit how you operate. Hope that helps.