September 1, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Using Visual Studio to Write Word Add-ins

  • August 4, 2009
  • By Jani Järvinen
  • Send Email »
  • More Articles »

Introduction

For a long time, businesses have relied on Microsoft's Office applications to help running their daily routines. Word is used to write e-mails and letters, Excel does the calculations, and Outlook communicates. Everyone has seen a slideshow made with PowerPoint. During the years, Office has grown, and today the number of applications part of the suite has grown to over ten.

Although many businesses can run their business with stock versions of these products, it is common to repeat the same steps again and again. For instance, sales people might copy and paste information from their sales system to a Word document, write their offer, send it out, and then return to the sales system to mark their offer as done. And then they start all over again.

As with many other Microsoft products, Office applications are also highly customizable. In the past few releases, extending these applications has become easier, and you as a developer are not anymore limited to writing COM DLLs with complex interfaces. Today, .NET developers can also easily get started.

In this article, you are going to learn how you can use Visual Studio 2008 and .NET to write custom add-ins for Word. Along the way, you will explore the possibilities for writing such applications. For instance, you can integrate databases, web services and even blogs to your add-ins.

Say hello to VSTO

Developing applications for the Office products, and especially for Office 2003 and 2007, is best done using Visual Studio Tools for Office, or VSTO. With VSTO in Visual Studio 2008, you can create different types of applications that utilize the Office products as a platform. For instance, you can create application-level add-ins, document-level customizations, templates, workflows, and so on. And as you might guess, you can develop your applications from the convenience of the Visual Studio IDE.

Love or hate it, the Office 2007 release bought developers the Ribbon (Figure 1). With VSTO, you can also create your own customizations to the ribbon, for example by adding buttons or creating new groups of commands. Although ribbon customizations are not the focus of this article, it is important to realize that this kind of user interface is getting more and more common. Windows 7 also contains a ribbon interface in many of the built-in applications such as Paint and Wordpad.



Click here for larger image

Figure 1. Office 2007 bought us a new user interface along with the Ribbon.

Office applications have had a user interface element called a task panel for a long time already (Figure 2). With VSTO, you can create your custom task panels, and for the purpose of this article, they are a great way to expose the add-in's user interface, but more on that shortly.



Click here for larger image

Figure 2. The Mail Merge feature uses a custom task panel to display its interface.

If you have studied Office application development before, you might have the impression that such development work is hard. With previous versions of Visual Studio, this arguably was the case, as you had to fiddle with different COM interfaces, primary interoperability assemblies (PIAs), and so on. But if you are using Visual Studio 2008 and are targeting Office 2007 applications, things have gotten much easier.

Installing VSTO usually happens along with Visual Studio. When selecting the features to be installed, you will also have the chance to select VSTO components (Figure 3). Note however, that VSTO is only available from Visual Studio Professional upwards. It is not available in the Visual Studio Standard or Expression editions, nor can it be purchased separately.



Click here for larger image

Figure 3 - Visual Studio Setup Screen.png

Figure 3. VSTO components are part of Visual Studio 2008 Professional and Team editions.

Creating a custom solution for Word

A technology like VSTO is best demonstrated with the help of a sample application. To demonstrate the possibilities of expanding Office applications with a custom solution, this article walks you through in creating an add-in for Word with VSTO in Visual Studio 2008.

The add-in itself contains a custom task panel. The purpose of this panel is to let a sales person send quotes to customers more efficiently. The sales person can simply enter a customer ID, and automatically fetch customer's basic information into the active Word document. The panel also contains a little utility to convert U.S. dollar amount to euros using a web service. Finally, once the document is saved, the add-in posts an update to a public blog. This way, other sales persons are aware of the updated quote.

To begin developing your custom task panel, you first need to fire up Visual Studio 2008. Choose to create a new project (with the File/New/Project menu command), and in the subsequent New Project dialog box, navigate to the Office/2007 project type node on the left (Figure 4). From there, select a Word Add-in, enter a name for your project (such as WordSalesToolsAddIn), and click OK.



Click here for larger image

Figure 4. Starting a Word add-in project is easy with the Office project templates.

Once Visual Studio has finished creating your project from the template, you should see a project with several files and references (Figure 5). For instance, under the Word folder, you should see a file named ThisAddIn.cs. This file acts like the Global.asax.cs file in ASP.NET web applications: the code in the file is executed when the add- in first loads, and also when add-in is unloaded, i.e. when Word closes down.



Click here for larger image

Figure 5. An empty add-in project as created by Visual Studio.

If you run the project immediately after creating it, Visual Studio will automatically register your add-in so that Word can load it, and will then start Word. This makes development and testing very easy. However, unless you have written additional code to the project, you cannot actually see anything in Word when you run your project. Even on the Ribbon, the Add-ins tab (if visible) doesn't contain any hints about your creation.

Of course, creating a custom task panel will create something that's visible in Word. At this point, you might be tempted to return to Visual Studio, open the Add New Item dialog box (Project/Add New Item) and look for a custom task panel object. Unfortunately, such an object does not exist. Instead, to create custom task panels for Word and other compatible Office applications, you need user controls.





Page 1 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel