NSIS VBA Add-In Installer

D

dgp

I recently tackled the job of writing an installer/uninstaller for a
VBA Add-in to distribute to my coworkers. I looked into using a VBA
installer, but was concerned that the default macro security settings
in Office 2003 might prevent the installer from executing. I wrote the
NSIS script below to directly register the Add-In as installed in the
Add-In Manager list. I'm new to using NSIS so it's not the most elegant
script but it does the job. I hope someone might find it useful.

Dave

; NSIS Excel Add-In Installer Script
; Include
!include MUI.nsh
!include LogicLib.nsh

; General
Name "NSIS Test"
OutFile "Setup.exe"
InstallDir "$PROGRAMFILES\NSIS Test"
InstallDirRegKey HKCU "Software\NSIS Test" "InstallDir" ;
Overrides InstallDir

; Interface Settings
!define MUI_ABORTWARNING

; Installer Pages
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

; Uninstaller Pages
!insertmacro MUI_UNPAGE_WELCOME
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_UNPAGE_FINISH

; Languages
!insertmacro MUI_LANGUAGE "English"

; Installer Section
Section "-Install"
SetOutPath "$INSTDIR"

; ADD FILES HERE
File "NSISTest.xla"
File "readme.txt"

; Check Installed Excel Version
ReadRegStr $1 HKCR "Excel.Application\CurVer" ""

${If} $1 == 'Excel.Application.8' ; Excel 95
StrCpy $2 "8.0"
${ElseIf} $1 == 'Excel.Application.9' ; Excel 2000
StrCpy $2 "9.0"
${ElseIf} $1 == 'Excel.Application.10' ; Excel XP
StrCpy $2 "10.0"
${ElseIf} $1 == 'Excel.Application.11' ; Excel 2003
StrCpy $2 "11.0"
${Else}
Abort "An appropriate version of Excel is not installed.
$\nNSIS Test setup will be canceled."
${EndIf}

; Find available "OPEN" key
StrCpy $3 ""
loop:
ReadRegStr $4 HKCU
"Software\Microsoft\Office\$2\Excel\Options" "OPEN$3"
${If} $4 == ""
; Available OPEN key found
${Else}
IntOp $3 $3 + 1
Goto loop
${EndIf}

; Write install data to registry
WriteRegStr HKCU "Software\NSIS Test" "InstallDir" $INSTDIR
; Install Directory
WriteRegStr HKCU "Software\NSIS Test" "ExcelCurVer" $2
; Current Excel Version

; Write key to install AddIn in Excel Addin Manager
WriteRegStr HKCU "Software\Microsoft\Office\$2\Excel\Options"
"OPEN$3" '"$INSTDIR\NSISTest.xla"'

; Write keys to uninstall
WriteRegStr HKLM
"Software\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Test"
"DisplayName" "NSIS Test"
WriteRegStr HKLM
"Software\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Test"
"UninstallString" '"$INSTDIR\uninstall.exe"'
WriteRegDWORD HKLM
"Software\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Test"
"NoModify" 1
WriteRegDWORD HKLM
"Software\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Test"
"NoRepair" 1

; Create uninstaller
WriteUninstaller "$INSTDIR\Uninstall.exe"
SectionEnd

; Uninstaller Section
Section "Uninstall"
; ADD FILES HERE...
Delete "$INSTDIR\NSISTest.xla"
;Delete "$INSTDIR\EngFunctHelp.chm"
Delete "$INSTDIR\readme.txt"
Delete "$INSTDIR\uninstall.exe"

RMDir "$INSTDIR"

; Find AddIn Manager Key and Delete
; AddIn Manager key name and location may have changed since
installation depending on actions taken by user in AddIn Manager.
; Need to search for the target AddIn key and delete if found.
ReadRegStr $2 HKCU "Software\NSIS Test" "ExcelCurVer"
StrCpy $3 ""

loop:
ReadRegStr $4 HKCU
"Software\Microsoft\Office\$2\Excel\Options" "OPEN$3"
${If} $4 == '"$INSTDIR\NSISTest.xla"'
; Found Key
DeleteRegValue HKCU
"Software\Microsoft\Office\$2\Excel\Options" "OPEN$3"
${ElseIf} $4 == ""
; Blank Key Found. Addin is no longer installed in
AddIn Manager.
; Need to delete Addin Manager Reference.
DeleteRegValue HKCU
"Software\Microsoft\Office\$2\Excel\Add-in Manager"
"$INSTDIR\NSISTest.xla"
${Else}
IntOp $3 $3 + 1
Goto loop
${EndIf}

DeleteRegKey HKCU "Software\NSIS Test"
DeleteRegKey HKLM
"Software\Microsoft\Windows\CurrentVersion\Uninstall\NSIS Test"
SectionEnd
 
R

RB Smissaert

Would it cope with multiple Excel versions?
I made an installer with INNO and just opted to offer a manual install if
there were multiple Excel versions.

RBS
 
D

dgp

It will install the Add-in in any of the following versions: 97, 2000,
XP, 2003. The installer determines which version installed by checking
the registry key HKCR\Excel.Application\CurVer.

If the current version key is not present (Excel not installed) or if
it does not match an appropriate version Excel97-Excel2003 the
installation will abort.

It will not install the add-in to multiple version of Excel running on
the same computer.

Dave Parker
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top