A Complete Shared Add-in Template


Over the years I have worked with a number of customers that still use/require the use of Shared Add-ins in Office. One customer I am working with today frequently has questions about implementations or encounters scenarios that are specific to Shared Add-ins.

If you have been working with Visual Studio for some time, you know there used to be a Shared Add-in template you could use to create an add-in. However, as of Visual Studio 2012, that template is now gone. Additionally, you might have found it was difficult to figure out how to implement Ribbons and TaskPanes.

With this in mind, I decided to publish on CodePlex a shared add-in template I have created for myself and that I think might be useful for others. The link is here:

Complete Shared Add-in C# Template

How to Use It

Once you load this solution, you will find the Connect.cs. This is the MAIN entry point for the DLL solution. The first thing you will want to do is define which applications this add-in will pertain to. This is done via the ThisAddin_Register() method. There is an object called “office” where you will define each application you want to hook to:

office.AddApplication(OfficeBase.APPTYPE.EXCEL, “Excel Shared Addin Example”, “Excel Shared Addin Example”);
office.AddApplication(OfficeBase.APPTYPE.WORD, “Word Shared Addin Example”, “Word Shared Addin Example”);
office.AddApplication(OfficeBase.APPTYPE.POWERPOINT, “PowerPoint Shared Addin Example”, “PowerPoint Shared Addin Example”);
office.AddApplication(OfficeBase.APPTYPE.OUTLOOK, “Outlook Shared Addin Example”, “Outlook Shared Addin Example”);

The AddApplication command will allow you to specify which application will load. And if you look in the IDTExtensibility2 code region you will find the RegisterFunction method. This method will setup/install this DLL (when registered via RegAsm), to work with COM Automation and also register it in each application that you setup in the ThisAddin_Register() method:

foreach(OfficeBase.OfficeAddinInfo info in office.GetAddins()) {}

This template already comes with Ribbons for each of the applications, Word, Excel, PowerPoint and Outlook. It also automatically loads each from the Resources.

NOTE: Outlook requires a bit more work than what is provided.

If you look at my previous post on how to create a TaskPane in a Shared Com Add-in, you will see that this has been incorporated in this sample as well. To add a task pane, you will define your control and then call like this:

SharedCustomTaskPane myPane = CustomTaskPaneCollection.Add(dynamicControl, “My Pane”);

You will also want to change the attributes at the top of the class:

[GuidAttribute(“11111111-AF27-4814-9CBE-ED6A39A4B9A5”),
ProgId(“SharedAddin.Connect”),
ClassInterface(ClassInterfaceType.AutoDual)]

When the add-in loads in a parent application, it will determine the type and then setup the “office” object so that it contains the reference to the parent application. So to get Excel, you can make a simple call like this:

Excel.Application xlApp = office.GetExcel();

Likewise, there is a similar function for Word, PowerPoint, and Outlook. From there you can attach to events and make additional calls as needed.

Hopefully, you will find this solution useful. It has been very useful to me over the years, especially as I have added to it. There are still many uses for this type of Add-in and having all your code in one place – across all the Office applications can be very useful.

One thought on “A Complete Shared Add-in Template

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s