Wednesday, August 20, 2008

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,

No comments: