Tuesday, April 17, 2007

Using Inno-Setup for Excel Add-In Installation

Recently, I came across a need to write an installer for Excel Add-In. I found a sample of installer written using Setup-Factory at (http://www.jkp-ads.com/articles/AddinsAndSetupFactory.asp) and that was my main inspiration source to write this in Inno-Setup. My current code is not completely cleaned up and doesn't include an option for adding the Add-In in the [Add-In Manager] registry key but it should do its job well. Here is the setup script:


[Setup]
AppName=My Excel Add-In
AppVerName=My Excel Add-In ver 1
DefaultDirName={pf}\Durlabh
[Files]
Source: MyExcelAddIn.xla; DestDir: {app}; AfterInstall: CheckRegistry(true);

[Code]
procedure CheckRegistry(Install: Boolean);
var
OfficeVersions: TArrayOfString;
I: Integer;
J: Integer;
Installed: Boolean;
Key: String;
Value: String;
Keys: TArrayOfString;
AlertMessage: String;
CurrentValue: String;
NewKey: String;
KeyNumber: Integer;
NewKeys: TArrayOfString;
begin
// Location of Add-In
Value := ExpandConstant('"{app}\MyExcelAddIn.xla"')

// List office versions
if RegGetSubkeyNames(HKEY_CURRENT_USER, 'Software\Microsoft\Office', OfficeVersions) then
begin

// Check all the office version
for I := 0 to GetArrayLength(OfficeVersions)-1 do
begin

// Initialize Installation Info
KeyNumber := 0;
Installed := false;

// Check if Excel is installed and has Options
Key := 'Software\Microsoft\Office\' + OfficeVersions[I] + '\Excel\Options';
if RegKeyExists(HKEY_CURRENT_USER, Key) then
begin

// List all the add-ins currently being shown - for this read all value name
if RegGetValueNames(HKEY_CURRENT_USER, Key, Keys) then
begin

// Process each value name
for J := 0 to GetArrayLength(Keys)-1 do
begin

// Check if it is really an ADD-IN
if (Length(Keys[J]) >= 4) AND (Copy(Keys[J], 1, 4) = 'OPEN') then
begin

// Read the add-in path
if RegQueryStringValue(HKEY_CURRENT_USER, Key, Keys[J], CurrentValue) then
begin

// Check if it is the add-in we are installing
if CompareText(Value, CurrentValue) = 0 then
Installed := true
else
begin

// Store all other add-ins in another array
// Will be used for uninstall
SetArrayLength(NewKeys, KeyNumber + 1);
NewKeys[KeyNumber] := CurrentValue;
KeyNumber := KeyNumber + 1;
end
end
end
end
end


if Installed then
begin
// Are we trying to uninstall?
if not Install then
begin

// Re-serialize all other add-ins
for J := 0 to KeyNumber-1 do
begin
NewKey := 'OPEN';
if J > 0 then
NewKey := NewKey + IntToStr(J);
RegWriteStringValue(HKEY_CURRENT_USER, Key, NewKey, NewKeys[J]);
end

// Delete additional keys
repeat
NewKey := 'OPEN';
if J > 0 then
NewKey := NewKey + IntToStr(J);

until (Not RegDeleteValue(HKEY_CURRENT_USER, Key, NewKey))
end
AlertMessage := 'Installed'
end
else // Not installed
begin
// We are trying to install - add to the last
if Install then
begin
NewKey := 'OPEN';
if KeyNumber > 0 then
NewKey := NewKey + IntToStr(KeyNumber);
RegWriteStringValue(HKEY_CURRENT_USER, Key, NewKey, Value);
end
AlertMessage := 'Not-Installed';
end
AlertMessage := 'For office version ' + OfficeVersions[I] + ', add-in is ' + AlertMessage;
//MsgBox(AlertMessage, mbInformation, MB_OK);
end
end
end
end;

procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);
begin
case CurUninstallStep of
usPostUninstall:
begin
CheckRegistry(false);
end;
end;
end;





Your comments/ feedback will be highly appreciated.

7 comments:

Anonymous said...

HI Durlabh,

Could you explain the uninstall part a bit more, If I read it correctly I think you're trying to set the addins list back to what it was before installation.

This means you will undo any changes to the installed addins the user has done in the mean time.

You should rather just remove your addin from the OPEN set of keys, Excel will reorder them automatically.

Durlabh Jain said...

Jan is right Excel will reorder the items but only after first time it is open. For example, let us say you have 10 add-ins and your add-in is at number 4. Once you remove your add-in from number 4, excel won't load rest of the add-ins. However, when you close, it will re-order and then next time all the add-ins will load correctly. That's why reordering in un-install was important for me.

Anonymous said...

Hello, Your work is excellent!

Merci

Anonymous said...

Could you tell me how to install the add-in for all users of Windows because I note that the add-in is installed only for the administrator?

Durlabh Jain said...

This is another great resource:

http://www.jkp-ads.com/Articles/AddinsAndSetupFactory.asp

There you can also find a way to install the add-in for all the users.

Sean said...

Fantastic example! Thanks for posting this!

Dan said...

great script!! works great with Windows XP and excel 2007.. and (I think) with Windows7 Excel 07. Any ideas on now I could mod it to install to excel 2010 on win7?