Skip to main content

Tutorial: How to Create an Add-in for Microsoft Outlook (part 2)

This post is the part 2 of Tutorial: How to Create an Add-in for Microsoft Outlook. In the following post, we'll see a few examples in we're using other object models, other than Outlook model.

Version on CodeProject

1. Examples With Other Models


Placing ourselves in context, when we are working with an email body, there are just a few functionalities we can perform using the Outlook objects, for example, accessing the Body properties of the MailItem object. But if it's needed to get more control over the email body, it's necessary to work with it as a Word document. Next, we'll see some examples about it.

Download source code

Before Starting

First, we'll see how to include the references to the Word Object Model from "Outlook 2013 and 2016 VSTO Add-in". It's important to select the same versions you're using in the Outlook application and also, select the references of tools and utilities.

In my case, I'm using the version 15.0.0.0 for Outlook.


Therefore, we'll select the same versions of the Word references.


1.1. How to Get the Selected Text in an Email Message from a Button on the Ribbon


The following example takes place in the OnClick event of a button added to the Ribbon (see section 4.3. How to add a control to the Ribbon toolbar, in part 1).

El siguiente ejemplo tiene lugar en el evento OnClick de un botón adicionado al Ribbon (ver ejemplo en la primera parte de este artículo).

private void button2Demo_Click(object sender, RibbonControlEventArgs e)
{
    // Get the Application object
    Outlook.Application application = Globals.ThisAddIn.Application;

    // Get the active Inspector object and check if is type of MailItem
    Outlook.Inspector inspector = application.ActiveInspector();
    Outlook.MailItem mailItem = inspector.CurrentItem as Outlook.MailItem;
    if (mailItem != null)
    {
        Word.Document document = (Word.Document) inspector.WordEditor;
        string selectedText = document.Application.Selection.Text;
        MessageBox.Show(selectedText);
    }
}

The following picture takes place when the user is creating a new email. In this, we can see that the application shows a message with the text where the user has selected and clicked on the "Get text selected" button.


1.2. How to Subscribe to Events Over the Email Body


In the following example, we'll demonstrate how to subscribe our application to the mouse's events that the user performs over an email body. Specifically, we'll subscribe to events from the Word document (that it represent the email body) and when the user performs a double-click on the text, we'll get the word where the click was done.

We'll start from the application entry point and we'll create an Inspector object to monitor when the user creates/edits an email.

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    // Get the Application object
    Outlook.Application application = this.Application;
    
    // Add a new Inspector
    inspectors.NewInspector += 
        new Outlook.InspectorsEvents_NewInspectorEventHandler(
            Inspectors_RegisterEventWordDocument);
}

With this Inspector, let's execute our code when the email editor is opened. At this time, we'll check that the Inspector object is a MailItem. Next, we'll check that the email editor is a Word editor (sometimes it is of another type) and, at last, we'll get the Word document object.

void Inspectors_RegisterEventWordDocument(Outlook.Inspector inspector)
{
    Outlook.MailItem mailItem = inspector.CurrentItem as Outlook.MailItem;
    if (mailItem != null)
    {
        // Check that the email editor is Word editor
        // Although "always" is a Word editor in Outlook 2013, it's best done perform this check
        if (inspector.EditorType == Outlook.OlEditorType.olEditorWord
            inspector.IsWordMail())
        {
            // Get the Word document
            Word.Document document = inspector.WordEditor;
            if (document != null)
            {
                // Subscribe to the BeforeDoubleClick event of the Word document

                document.Application.WindowBeforeDoubleClick += 
                    new Word.ApplicationEvents4_WindowBeforeDoubleClickEventHandler(
                        ApplicationOnWindowBeforeDoubleClick);
            }
        }
    }
}

Next, we're going to subscribe to the BeforeDoubleClick event of the Word document that we got (in previous code) and when the event is triggered, we'll select the word where the user has clicked.

private void ApplicationOnWindowBeforeDoubleClick(Word.Selection selection,
    ref bool cancel)
{
    // Get the selected word
    Word.Words words = selection.Words;
    MessageBox.Show("Selection: " + words.First.Text);
}

We can get access to the user selected words by the selection object, which it has a lot of functionalities. In our example, using the Word property, we get a collection of all the selected words by the user and, the First property is where the user has clicked.

The following picture takes place when the user is creating a new email. In this, we can see that the application shows a message with the text where the user has done a double-click.


2. Conclusion


As can be seen, we can extend the Outlook functionalities in many ways and easily, using the VSTO Add-in tools. In my opinion, there're many requirements to this type of application in the enterprise sectors that resolve some kind of problems in a quick way, and also, usually the office users feel comfortable with Microsoft Office products than others applications. With VSTO Add-in, we can query databases to get employee's contacts, product list to include on emails, synchronize appointments between an enterprise application and the Outlook, and so on.

3. References

Comments

Popular posts from this blog

How to use Font Awesome with IText

This post is about on how to use Font Awesome with IText library to generate awesome icons in our PDF documents. In this post, we are using  ITextSharp , a C# port for IText, but those are the same functionalities.

TinyMCE: anti-forgery token failed on file upload in an ASP.NET Boilerplate application

TinyMCE is an excellent full featured and well documented web editor that we can use in our web applications. But, integration with ASP.NET MVC 5 (as part of the ASP.NET Boilerplate technology stack) may be tricky to accomplish. When you set up the file upload plugin in TinyMCE, you have to consider the ASP.NET MVC 5 anti-forgery system. Otherwise, HTTP requests may fail loading resources to server due to an invalid or empty anti-forgery token error. This is a security and technology concern that can run out your time if you don't know about it. This article explains how this mechanism works and shows some solutions in an ASP.NET Boilerplate application.