Tuesday, June 16, 2020

Installation and uninstallation of an Excel addin

Case I Installation of an Excel addin

Sub InstallYourAddin()
    Dim oFSO As Object
    Dim oFD As Office.FileDialog
    Dim strSourcePath As String
    Dim strAddInsName As String
    
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oFD = Application.FileDialog(msoFileDialogFilePicker)
    
    
    With oFD
        .Filters.Clear 'clear old filters, if any
        ''allow to select only .xlam extension files i.e. addin files
        .Filters.Add "Excel Addin Files", "*.xlam?", 1
        .AllowMultiSelect = False   '' allow single file selection
        .Title = "Select the addin file to install"
        .InitialFileName = ActiveWorkbook.Path
        If .Show = True Then
            strSourcePath = .SelectedItems(1)    ' Get the full path
            fileNamewithextn = Dir(.SelectedItems(1))    ' Get the full path
        Else
            Exit Sub
        End If
    End With
    
    TargetAddinFolder = Application.UserLibraryPath ''path separator is appended at the end

    'Check if add-in is installed
    
    strAddInsName = Left$(fileNamewithextn, Len(fileNamewithextn) - 5)
    
    If oFSO.FileExists(TargetAddinFolder & fileNamewithextn) Then
        If AddIns(strAddInsName).Installed = False Then
            AddIns(strAddInsName).Installed = True
            MsgBox "Add-in is installed successfully.", vbInformation, "Installation"
            Exit Sub
        Else
            'deactivate the add-in if it is activated
            If AddIns(strAddInsName).Installed = True Then
                AddIns(strAddInsName).Installed = False
            End If
            
            'delete the old file
            oFSO.DeleteFile TargetAddinFolder & fileNamewithextn, True
            
            'copy the new file
            oFSO.Copyfile strSourcePath, TargetAddinFolder
            AddIns(strAddInsName).Installed = True
            
            MsgBox "Addin is replaced successfully.", vbInformation, "Installation"
        End If
    Else
        'copy the new file
        oFSO.Copyfile strSourcePath, TargetAddinFolder
        AddIns(strAddInsName).Installed = True
        MsgBox "New add-in is installed successfully.", vbInformation, "Installation"
    End If
End Sub

Case II Uninstallation of an Excel addin

Sub UninstallAddin()
    Dim myAddin
    Dim blnAddInFound As Boolean
    
    blnAddInFound = False   '' also, boolean variable by default is False
    
    For Each myAddin In Application.AddIns
        If LCase(myAddin.Name) = LCase("Sales.xlam") Then
            ''uncheck the installation state
            myAddin.Installed = False
            blnAddInFound = True
            Exit For
        End If
    Next myAddin
    
    If blnAddInFound Then
        MsgBox "Addin is uninstalled successfully.", vbInformation, "Uninstalled"
    Else
        MsgBox "Addin not found.", vbCritical, "Uninstallation"
    End If

End Sub


No comments:

Post a Comment

Hot Topics