Compartir a través de


Walkthrough: Creating Your First Application-Level Add-in for Outlook

This walkthrough shows you how to create an application-level add-in for Microsoft Office Outlook. The features that you create in this kind of solution are available to the application itself, regardless of which Outlook item is open. For more information, see Office Solutions Development Overview.

Applies to: The information in this topic applies to application-level projects for Outlook 2013 and Outlook 2010. For more information, see Features Available by Office Application and Project Type.

This walkthrough illustrates the following tasks:

  • Creating an Outlook add-in project for Outlook.

  • Writing code that uses the object model of Outlook to add text to the subject and body of a new mail message.

  • Building and running the project to test it.

  • Cleaning up the completed project so that the add-in no longer runs automatically on your development computer.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings in Visual Studio.

Prerequisites

You need the following components to complete this walkthrough:

Creating the Project

To create a new Outlook project in Visual Studio

  1. Start Visual Studio.

  2. On the File menu, point to New, and then click Project.

  3. In the templates pane, expand Visual C# or Visual Basic, and then expand Office/SharePoint.

  4. Under the expanded Office/SharePoint node, select the Office Add-ins node.

  5. In the list of project templates, select Outlook 2010 Add-in or Outlook 2013 Add-in.

  6. In the Name box, type FirstOutlookAddIn.

  7. Click OK.

    Visual Studio creates the FirstOutlookAddIn project and opens the ThisAddIn code file in the editor.

Writing Code that Adds Text to Each New Mail Message

Next, add code to the ThisAddIn code file. The new code uses the object model of Outlook to add text to each new mail message. By default, the ThisAddIn code file contains the following generated code:

  • A partial definition of the ThisAddIn class. This class provides an entry point for your code and provides access to the object model of Outlook. For more information, see Programming Application-Level Add-Ins. The remainder of the ThisAddIn class is defined in a hidden code file that you should not modify.

  • The ThisAddIn_Startup and ThisAddIn_Shutdown event handlers. These event handlers are called when Outlook loads and unloads your add-in. Use these event handlers to initialize your add-in when it is loaded, and to clean up resources used by your add-in when it is unloaded. For more information, see Events in Office Projects.

To add text to the subject and body of each new mail message

  1. In the ThisAddIn code file, declare a field named inspectors in the ThisAddIn class. The inspectors field maintains a reference to the collection of Inspector windows in the current Outlook instance. This reference prevents the garbage collector from freeing the memory that contains the event handler for the NewInspector event.

    Private WithEvents inspectors As Outlook.Inspectors
    
    Outlook.Inspectors inspectors;
    
  2. Replace the ThisAddIn_Startup method with the following code. This code attaches an event handler to the NewInspector event.

    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        inspectors = Me.Application.Inspectors
    End Sub
    
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        inspectors = this.Application.Inspectors;
        inspectors.NewInspector +=
        new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
    }
    
  3. In the ThisAddIn code file, add the following code to the ThisAddIn class. This code defines an event handler for the NewInspector event.

    When the user creates a new mail message, this event handler adds text to the subject line and body of the message.

    Private Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles inspectors.NewInspector
        Dim mailItem As Outlook.MailItem = TryCast(Inspector.CurrentItem, Outlook.MailItem)
        If Not (mailItem Is Nothing) Then 
            If mailItem.EntryID Is Nothing Then
                mailItem.Subject = "This text was added by using code"
                mailItem.Body = "This text was added by using code" 
            End If 
        End If 
    End Sub
    
    void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector)
    {
        Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
        if (mailItem != null)
        {
            if (mailItem.EntryID == null)
            {
                mailItem.Subject = "This text was added by using code";
                mailItem.Body = "This text was added by using code";
            }
    
        }
    }
    

To modify each new mail message, the previous code examples use the following objects:

  • The Application field of the ThisAddIn class. The Application field returns an Application object, which represents the current instance of Outlook.

  • The Inspector parameter of the event handler for the NewInspector event. The Inspector parameter is an Inspector object, which represents the Inspector window of the new mail message. For more information, see Outlook Solutions.

Testing the Project

When you build and run the project, verify that the text appears in the subject line and body of a new mail message.

To test the project

  1. Press F5 to build and run your project.

    When you build the project, the code is compiled into an assembly that is included in the build output folder for the project. Visual Studio also creates a set of registry entries that enable Outlook to discover and load the add-in, and it configures the security settings on the development computer to enable the add-in to run. For more information, see Walkthrough: Creating Your First Application-Level Add-in for Outlook.

  2. In Outlook, create a new mail message.

  3. Verify that the following text is added to both the subject line and body of the message.

    This text was added by using code.

  4. Close Outlook.

Cleaning up the Project

When you finish developing a project, remove the add-in assembly, registry entries, and security settings from your development computer. Otherwise, the add-in will run every time that you open Outlook on the development computer.

To clean up your project

  • In Visual Studio, on the Build menu, click Clean Solution.

Next Steps

Now that you have created a basic application-level add-in for Outlook, you can learn more about how to develop add-ins from these topics:

See Also

Other Resources

Programming Application-Level Add-Ins

Outlook Solutions

Office UI Customization

Building Office Solutions

Deploying an Office Solution

Office Project Templates Overview