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 Object, ByVal 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 Object, ByVal 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 Object, ByVal 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.