Home » excel » Uninstalling Excel add-in using VBScript

Uninstalling Excel add-in using VBScript

Posted by: admin April 23, 2020 Leave a comment

Questions:

I’m trying to create a MSI installer that installs an Add-In (.xla) into Microsoft Excel (2007 in my case). Installing it goes well. I use a ‘Custom Action’ that runs this VBScript file:

Dim SourceDir
Dim objExcel
Dim objAddin

SourceDir = Session.Property("CustomActionData")
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Add
Set objAddin = objExcel.AddIns.Add(SourceDir & "addin.xla", True)
objAddin.Installed = True
objExcel.Quit
Set objExcel = Nothing

I pass the location of the addin to the script using the CustomActionData property. The Add-in is copied to a folder inside ‘Program Files’, where it will stay until it is uninstalled. This is handled by the installer itself.

The problem is when I use the uninstall script:

Dim objExcel
Dim addin
On Error Resume Next

Set objExcel = CreateObject("Excel.Application")
For i = 0 To objExcel.Addins.Count
    Set objAddin= objExcel.Addins.item(i)
    If objAddin.Name = "addin.xla" Then
        objAddin.Installed = False
    End If
Next

objExcel.Quit
Set objExcel = Nothing

The addin creates a custom toolbar in Excel u[ installation. The toolbar is not removed upon uninstall, and the entry of the add-in in the ‘Add-in’ section of Excel’s settings isn’t either.

Can anyone tell me if these two things can be done programmatically using VBScript?

thanks in advance

How to&Answers:

Almost all collections in VB(A) are 1-based. Your loop goes from zero, and so it’ll fail when it tries to access AddIns(0). That’s masked by the fact that you have On Error Resume Next.

Also, you’ll need to explicitly remove the toolbar. Simply removing the add-in won’t do that.

I can’t say I like the way you’re doing it, but it should work at least 50% of the time 🙂

Answer:

For Each Key in Keys
    objReg.EnumKey HKEY_CURRENT_USER,strRegPath & "\" & Key,xlKeys
    For Each xlKey in xlKeys
        if(LCase(xlKey) = "excel") Then
            objReg.EnumKey HKEY_CURRENT_USER,strRegPath & "\" & Key & "\" & xlKey,subKeys

            If(Not ISNULL(subKeys))  Then
                                For Each subKey in subKeys
                    If(lcase(subKey) = "options") Then          'Find Options subKey
                        objReg.EnumValues HKEY_CURRENT_USER, strRegPath & "\" & Key & "\" & xlKey & "\" & subKey, Values
                        If (Not IsNull(Values)) Then
                                     For Each oValue In Values
                                If (LCase(Left(oValue, 4))="open") Then
                                    objReg.GetStringValue HKEY_CURRENT_USER, strRegPath & "\" & Key & "\" & xlKey & "\" & subKey & "\", oValue, sTempValue
                                    If(Not XLAddinRemove(sTempValue)) Then

                                                                                If(aOpenKeyVals(0)<>"") Then
                                            Redim Preserve aOpenKeyVals(UBound(aOpenKeyVals)+1)
                                        End If
                                        aOpenKeyVals(UBound(aOpenKeyVals))=sTempValue
                                                                        End If
                                    objReg.DeleteValue HKEY_CURRENT_USER, strRegPath & "\" & Key & "\" & xlKey & "\" & subKey, oValue
                                End If
                            Next
                            For iOLoop = 0 To UBound(aOpenKeyVals)
                                If(iOLoop>0) Then sOpenName = "OPEN" & iOLoop
                                If aOpenKeyVals(iOLoop) <> "" Then
                                                                  objReg.SetStringValue HKEY_CURRENT_USER, strRegPath & "\" & Key & "\" & xlKey & "\" & subKey, sOpenName, aOpenKeyVals(iOLoop)
                                                                End If 
                            Next

                            sOpenName="OPEN"
                            Redim aOpenKeyVals (0)
                            aOpenKeyVals (0)=""
                        End If
                    End If
                 Next
            End If
          End If
    Next
Next