Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel add-in installer 1

Status
Not open for further replies.

combo

Technical User
Jan 1, 2003
4,188
PL
Here's the way I implemented to distribute excel add-in file.
Necessary tools:
- file (1) - excel add-in ("addin.xla"), to be updated/installed,
- file (2) - excel file ("TheFile2.xls") with supplementary code,
- installer, here used free Inno Setup from
How it works:
1. installer: copies files (1) and (2) to temporary directory and runs (opens) file (2),
2. file (2): Workbook_Open event - checks add-ins, uninstalls file (1) if exists and is installed, copies file (1), deletes its temporary version and installs add-in,
3. file (1): Workbook_AddinInstall event - check if has to finish instalation, if so - closes file (2) and deletes it.

Macros have to be enabled.

Both workbook (1) and workbook (2) require reference to Micresoft Scripting Runtime.


The code:

Workbook (1):
ThisWorkbook module:

Private Sub Workbook_AddinInstall()
Dim FSO As Scripting.FileSystemObject
Dim sName As String, sFullName As String, bIsInReg As Boolean

' read from registry information about excel installing file
bIsInReg = GetSetting(appname:="Xl_InstallAddIn", section:="DelInfo", Key:="IsInTemp", Default:=False)

' if marked to delete - do this, first close it
If bIsInReg Then
Set FSO = New Scripting.FileSystemObject
sFullName = GetSetting(appname:="Xl_InstallAddIn", section:="DelInfo", Key:="FileName")
sName = GetSetting(appname:="Xl_InstallAddIn", section:="DelInfo", Key:="FileNameShort")
Workbooks(sName).Close savechanges:=False
FSO.DeleteFile (sName)
DeleteSetting "Xl_InstallAddIn", "DelInfo"
Set FSO = Nothing
End If
End Sub


Workbook (2)
ThisWorkbook module:

Private Sub Workbook_Open()
Dim FSO As Scripting.FileSystemObject
Dim TempFol As Folder, DestFol As Folder
Dim AddinFile As File
Dim oAddIn As AddIn
Dim sTemp As String, sDest As String
Dim bIsAddIn As Boolean

' add-in's file name
Const sAddinFile As String = "addin.xla"

bIsAddIn = False
Set FSO = New Scripting.FileSystemObject

' check if add-in exists and uninstall it (to replace it)
With Application
For Each oAddIn In .AddIns
If oAddIn.Name = sAddinFile Then
bIsAddIn = True
oAddIn.Installed = False
Exit For
End If
Next oAddIn
End With

' copy add-in, with overwriting old version
' add-in and this code are in the same folder
sTemp = ThisWorkbook.Path & "\" & sAddinFile
With FSO
Set DestFol = .GetFolder(Application.UserLibraryPath)
sDest = DestFol & "\" & sAddinFile
.CopyFile sTemp, sDest, True
.DeleteFile sTemp
End With
Set FSO = Nothing
Set DestFol = Nothing

' VB and VBA key in registry
' mark file with this code to delete while installing the add-in
SaveSetting appname:="Xl_InstallAddIn", section:="DelInfo", Key:="FileName", setting:=ThisWorkbook.FullName
SaveSetting appname:="Xl_InstallAddIn", section:="DelInfo", Key:="FileNameShort", setting:=ThisWorkbook.Name
SaveSetting appname:="Xl_InstallAddIn", section:="DelInfo", Key:="IsInTemp", setting:=True

' adding & installing the add-in
If Not bIsAddIn Then
Set oAddIn = AddIns.Add(sAddinFile)
End If
oAddIn.Installed = True
End Sub


The Inno Setup script:
[Setup]
MessagesFile=compiler:default.isl

AppName=Excel add-in
AppVerName=v. 1.0 beta
AppPublisher=Me
DisableAppendDir=yes
DisableDirPage=yes

DefaultDirName={tmp}
AppCopyright=Me
CreateUninstallRegKey=no
AllowRootDirectory=yes
PrivilegesRequired=none
Uninstallable=no
UsePreviousAppDir=no
OutputBaseFilename=SetupFileName
WizardImageFile=MyPicture.bmp

[Files]
Source: C:\FullPath\addin.xla; DestDir: {tmp}; Flags: confirmoverwrite
Source: C:\FullPath\TheFile2.xls; DestDir: {tmp}; Flags: confirmoverwrite

[Messages]
BeveledLabel=My add-in

[Run]
Filename: "{tmp}\TheFile2.xls"; Flags: shellexec


combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top