AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1758&pId=-1
Creating Outlook Plug-In Using Visual Studio Tools for Office
page
by Anuj Sahu
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 34745/ 36

Introduction

VSTO stands for Visual Studio Tools for Office. It is a free add-on for Visual Studio 2005. The latest version of VSTO 2005 SE (build 8.0.50727.940) can be downloaded from the website of Microsoft. By default, VSTO 3.0 gets installed with all versions of Visual Studio 2008, except Express and Standard editions.

Prior to VSTO, unmanaged Component Object Model (COM) Office applications add-ins were created using VBA or managed Component Object Model (COM) Office applications add-in were created using shared add-ins wizard of Visual Studio.

VSTO provides the flexibility to the developer for developing Microsoft Office 2003/2007 based custom application with some of the great new stunning features which were lacking in VBA/Shared add-in. Some of the features and benefits of using VSTO are:

1.    Design time support

2.    Document level customization for Word and Excel

3.    Application level customization for Outlook

4.    Ease of application deployment and maintenance

5.    Enhanced security feature of VSTO helps and protects User from viruses and malicious code

6.    Full support of features provided by the Microsoft .NET Framework because of Managed code

Creating Outlook Add-in

From here I am continuing with VSTO SE installed as Add-on on Visual Studio 2005. The add-in created below is not very interesting, but it demonstrates how to start and create Outlook add-in using VSTO.

1. After installing VSTO SE in the new project dialog box, you will notice one new section 2003 Add-ins, 2007 Add-ins under Visual Basic and Visual C# project type.

Figure 1: New project types - 2003 Add-in and 2007 Add-in

2. Here we will be creating an Outlook 2007 Add-in using Visual Basic. So select Outlook Add-in project and name it as "OutlookAddIn."

Please note that you should have Outlook 2007 installed on your system. In case you have Outlook 2003, select Outlook Add-in from Office 2003 Add-ins menu.

3. After clicking OK you will notice two projects being added into the project Solution.

·         OutlookAddIn -- This project contains a class named ThisAddIn. The ThisAddIn class provides a starting location in which we will begin writing our codes.

·         OutlookAddInSetup -- This is a windows setup project for deploying the application, gets automatically created and added while creating new add-in project using VSTO.

4. Open the file "ThisAddIn.vb" in the project OutlookAddIn. You will find two methods here:

Listing 1

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Startup
End Sub
Private Sub ThisAddIn_Shutdown(ByVal sender As ObjectByVal e As System.EventArgs) _
Handles Me.Shutdown
End Sub

ThisAddIn_Startup method is called at application Outlook startup and ThisAddIn_Shutdown when outlook gets shut down.

 

5. We will be creating a simple application which will display a message box "Message at Outlook start-up" at Outlook start-up, a message box displaying the name of the folder on folder switch and a message box "Message at Outlook shut-down" at Outlook shut-down.

In your ThisAddIn class add the following code.

Listing 2

Public Class ThisAddIn
Private WithEvents Active_Explorer As Microsoft.Office.Interop.Outlook.Explorer
Private Sub ThisAddIn_Startup(ByVal sender As ObjectByVal e As System.EventArgs) _
Handles Me.Startup
System.Windows.Forms.MessageBox.Show("Message at Outlook start-up.")
Active_Explorer = Me.Application.ActiveExplorer
End Sub
Private Sub ThisAddIn_Shutdown(ByVal sender As ObjectByVal e As System.EventArgs) _
Handles Me.Shutdown
System.Windows.Forms.MessageBox.Show("Message at Outlook shut-down.")
End Sub
Private Sub ActiveExplorer_FolderSwitch() Handles Active_Explorer.FolderSwitch
System.Windows.Forms.MessageBox.Show(Active_Explorer.CurrentFolder.Name)
End Sub
End Class 

In the listing above, a message box is added in the method ThisAddIn_Startup to display "Message at Outlook start-up" when your Outlook application starts.

We have a global variable Active_Explorer of type Microsoft.Office.Interop.Outlook.Explorer, the window for displaying the content of folders item in Outlook. This variable Active_Explorer is assigned with the value of current Outlook application's active window at Outlook start-up.

Method ActiveExplorer_FolderSwitch handles the folder switch in the Outlook window and displays the selected folder name in a message box.

Method ThisAddIn_Shutdown is called at application shut down. Here a message box "Message at Outlook shut-down" is displayed.

Press F5 to start debugging the project. Outlook will be started and at start-up a message box will be displayed. Select a different folder from the folder list panel, a message box will display the name of the selected folder. Now close Outlook, again a message box will be displayed.

Deploying Outlook Add-in

To deploy VSTO application, a user's computer must have the following softwares installed:

1. .NET Framework 2.0

2. Appropriate version of Office 2003/2007.

3. Office Primary Interop Assemblies.

4. VSTO 2005 SE runtime.

5. Visual Studio Tools for Office Language Pack (optional)

Apart from these you must grant trust to the assemblies for the solution so that the .NET Framework allows them to execute.

To know how to include prerequisite in your installer and how to grant trust to assemblies for your solution, please visit http://msdn.microsoft.com/en-us/library/bb332051.aspx.

References
Conclusion

This is simple article demonstrating on how to start programming add-in applications for Microsoft Office 2003/2007 using the latest version of VSTO SE. There is lot more that you can do to customize and make your Office application more powerful.


Product Spotlight
Product Spotlight 

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 11:18:13 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search