Pieter van der Westhuizen

How to allow a user to choose target MS Office applications during installation

In today’s post I’ll demonstrate how you can customize your add-in setup project to allow your users to select which Microsoft Office applications the add-in will be installed for. I use Add-in Express 2010 for Office and .net.

Start by creating a new ADX COM Add-in project in Visual Studio. Select your preferred programming language and the minimum supported Office version and click the Next button. Choose Microsoft Excel and Word in the list of supported applications and complete the New Microsoft Office COM Add-in wizard.

Add the required functionality to your add-in and create a setup project by clicking on the project and selecting Create Setup Project from the Visual Studio Project menu. You can also add a setup project by right-clicking on the project name in the Solution Explorer and selecting Create Setup Project from the context menu.

Creating a new setup project

The New Setup Project wizard will be displayed, complete the necessary fields and click the Next button.

New Setup Project wizard - specifying title, description

Choose your preferred localization, file name and output directory and click Finish.

New Setup Project wizard - choosing localization, file name and output directory

The wizard will automatically add the Project Output for the add-in to the setup. Next, select the setup project in Solution Explorer and click the User Interface Editor button on the Solution Explorer toolbar. Add a new dialog by right-clicking on the Start node and selecting Add Dialog from the context menu.

Adding a new dialog

Select the RadioButtons (3 buttons) dialog and click OK. Drag the new dialog between the Welcome and Installation Folder dialogs.

The RadioButtons (3 Buttons) dialog is added

Select the RadioButtons (3 Buttons) dialog and set its properties to the following:

  • BannerText : Select supported MS Office Applications
  • BodyText: Select the MS Office application that should be supported by this add-in:
  • Button1Label : Microsoft Excel
  • Button1Value: 1
  • Button2Label: Microsoft Word
  • Button2Value: 2
  • Button3Label: Both
  • Button3Value: 3
  • ButtonProperty: SELECTEDAPP
  • DefaultValue: 1

Next, edit the adxloader.dll.manifest file by right-clicking on it in the Solution Explorer and choosing Open from the context menu. Change the xml to:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <assemblyIdentity name="ChooseTargetAppsDemo, PublicKeyToken=11afd0a459244c03">
    <addinName>ChooseTargetAppsDemo</addinName>
    <supportedApps>1</supportedApps>
    <displayAlerts>true</displayAlerts>
    <description>Choose Target Office Applications Demo</description>
    <loadBehavior>3</loadBehavior>
  </assemblyIdentity>
  <loaderSettings generateLogFile="true"
                     shadowCopyEnabled="true"
					 privileges="user"
					 configFileName="App.config" />
</configuration>

We’lll use the supportedApps value to specify which MS Office our add-in will target. The values for the supportedApps are taken from AddinExpress.MSO.ADXOfficeHostApp and are:

  • Unknown = 0
  • Microsoft Excel = 1
  • Microsoft Word = 2
  • Microsoft Outlook = 4
  • Microsoft PowerPoint = 8
  • Microsoft Access = 16
  • Microsoft Project = 32
  • Microsoft FrontPage = 64
  • Microsoft MapPoint = 128
  • Microsoft Visio = 256
  • Microsoft Publisher = 512
  • Microsoft InfoPath = 1024

Next, we need to create a custom action in order to set the supportedApps property. In order to do this, add a new Class Library project to the solution. Name it TargetAppsCustomAction and add a new Installer class to the project.

Adding a new Installer class to the project

Switch to the Installers class’s code view and add the following code:

public override void Install(IDictionary stateSaver)
{
	string targetApp = Context.Parameters["targetApp"];
	string targetPath = Context.Parameters["targetdir"];
	targetPath += "adxloader.dll.manifest";
 
	XmlDocument doc = new XmlDocument();
	doc.Load(targetPath);
	XmlNode supportedAppsNode = doc.SelectSingleNode(
		"configuration/assemblyIdentity/supportedApps");
	supportedAppsNode.InnerText = targetApp;
	doc.Save(targetPath);
 
	base.Install(stateSaver);
}

Build the class library and add a new file to the setup project. Browse to the class library’s bin folder and select its dll. Open the Custom Actions editor by clicking on its icon on the Solution Explorer toolbar. Right-click on the Install node and select Add Custom Action… from the context menu. Select the class library dll from the Application Folder.

Adding the custom action assembly

Move the custom action above the adxregister.exe action by right-clicking on it and selecting Move Up from the context menu. Next, we need to pass the target application the user has selected as well as the installation folder of the add-in, to our custom action. To do this, select the custom action and change its CustomActionData property to /targetApp=[SELECTEDAPP] /targetdir=”[TARGETDIR]\”

Build your setup project and install it. You should be prompted to select the supported MS Office application. Experiment with the options to test the target application.

Selecting the supported MS Office application

Thank you for reading. Until next time, keep coding!

Available downloads:

This sample add-in was developed using Add-in Express for Office and .net:
C# sample add-in

Post a comment

Have any questions? Ask us right now!