Friday, August 22, 2008

AddIn Part 5 : Cusom Actions on AddIn Installation

Apart from basic functionality of the AddIn, we also want to copy some of the files like text and images for use in our AddIn when our AddIn gets installed on client system.

This can be achieved using Setup project functionality - Custom Actions.

First of all, let’s add one class library project to the solution and name it say FileHandler and add one Installer class to this project. After this step, our solution structure looks like:

Being an Installer class, we can override different methods on Handler class like, OnAfterInstall, OnAfterUninstall, OnAfterRollback, OnBeforeInstall, OnBeforeUninstall and OnCommited.

In our case we will override OnAfterInstall and will write the code to create one text file (overwrite if exists) on client system and in OnAfterUninstall event, we will delete this file.

As the code is ready, now we have to assign this module to Installer. Right click the AddIn Setup project and go to View -> Custom Actions.

In the resulting window, select Install and Right click and then Add Custom Action...

You will see “Select item in Project” dialog, click Add Output…, and set the Primary output of FileHandler using figure shown below

Click Ok.

We should set the same handler for the Uninstall also using same steps above as we want the file to be deleted when this AddIn is uninstalled.

Now when you install this AddIn, it will create one text file on client computer and will get deleted on uninstalling.

Wednesday, August 20, 2008

AddIn Part 4 : Debugging MS Word AddIn

As with other project types, we also need to debug AddIn project as and when AddIn loads or any of the event fires for command bars and buttons.

Below are the steps for debugging code in AddIn project:

Right Click the AddIn project and go to Properties

In the Debug tab, under Start Action, select Start external program and set the WINWORD.exe process path. Shown in figure below

Save the project file

Set the breakpoint in code and press F5 to debug.

When it runs, MS Word will be opened and breakpoint will hit when it reaches location.


AddIn Part 3 : Registry settings for the AddIn

As we discussed earlier, we can also set LoadBehaviour for the AddIn while development also.

For that Right Click on AddIn Setup project, select View->Registry.

In the resulting window, expand the entries such as shown below figure.

You can change properties for the AddIn in Properties window.

Select the AddIn and press F4 for the Properties.

Now when you install this AddIn, these settings will remain along with installation.

AddIn Part 2 : Creating an Add-In with Visual Studio

Here is how we are going to create our AddIn to MS Word.
...

We assume that our development machine has all the prerequisites (discussed in earlier post) to develop this AddIn.

Start with creating an Extensibility type of project in VS 2005 with Shared AddIn template.

Follow through the wizard..

-Name the AddIn,
-Choose the MS Office Applications that you want to create for. Word in our case.

You will see two projects in your Solution Explorer.

1. AddIn Project of Class library type
2. Setup project for the AddIn

You will need to add the reference Microsoft.Office.Interop.Word and also for Microsoft Office 12.0 Object Library (Office.dll).

AddIn Project has one file Connect.cs, which will have different events for the AddIn.

Open the file and look for OnStartupComplete and put the code pasted below. (Click on Image for larger view)



And don't forget to add the event handler for button's click event to show message box.
private void btnSayHello_Click(CommandBarButton cmdBarbutton, ref bool cancel)
{
MessageBox.Show("Hello World...");
}

It's time to compile the project.

Now Right click Setup project, and click Install. This will install our AddIn to MS Word.
If MS Word meets the required settings for the Add-In, then a custom toolbar with button "Wish Me!!!" will appear.

Click the button and here is your "Hello World...".

Now you add more functionalities to this AddIn application.
You can add few more Forms, images and other files and give an applicaiton a nice user interface.

To load the inital form WishMeForm, for example when you click the above Wish Me!!! button,
you can write the code in click event:

WishMeForm myForm = new WishMeForm();
myForm.Show();

Here in that form, you can have any Windows forms controls like any other Win Application.

Cheers,

AddIn Part 1 : Prerequisites for Dev and Client system

There are certain prerequisites for the development and client system environment to have the functionalities of our Custom toolbars and AddIns.

Client System
Below are few things that can prevent this custom toolbar to appear in MS Word 2003.
1. Microsof .Net Framework 1.1
2. Should have .Net Programmability Support component for MS Office application installed or full install of MS Office 2003.
3. Macro Security settings in Word. Should be set to Medium. You can veryify that at Tools -> Macros -> Security...
4. Should not be in the list of 'Disabled items'. You can find this in Word 2003 by going to Help -> About Microsoft Word 2003.
5. LoadBehaviour for this Addin in Registry should be set to 2. You can verify that in registry for this Add-In at

HKEY_CURRENT_USER\Software\Microsoft\Office\Word\Addins\{AddinName}

There are different values you can set for this LoadBehaviour :

0 = Disconnect - Is not loaded.

1 = Connected - Is loaded.

2 = Bootload - Load on application startup.

8 = DemandLoad - Load only when requested by user.


Development Environment
1. Recommended that it has full install of MS Office (Word)
2. Macro Security settings in Word. Should be set to Medium.
Once you develop and to test that in the same system you need to verify the settings 4 & 5 above.
We also understand that dev system must have Visual Studio and .Net framework installed.

Note: Setting - 5 can be set while development of AddIn in visual studio and if that is so then that setting goes to client system so you don't need to set explicity there. More about this will be in the coming posts.