How to create VSTO Add-in for Outlook 2013 and 2016

An add-in is a software program that increases the capabilities of bigger applications like Microsoft Word, Excel, Outlook, etc. An add-in has specific but limited features that require only minimal memory resources. Once the add-in is installed, it becomes part of the larger program and runs along with it.

Steps for creating VSTO Add-in for Outlook 2013 and 2016

1. Open Visual Studio 2017 and create a new project by clicking on File→New->Project.

Give a proper name for the add-in in Name Textbox, and then choose a location from where you want to keep your add-in application. Finally, click on the OK button.

A new Addin.cs page opens with two methods:

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

// must run when Outlook Start up

}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)

{

// Note: Outlook no longer raises this event. If you have code that

// must run when Outlook shuts down, 

}

I am creating a simple add-in that sends some notification message to the user’s personal email id whenever a new email is coming in their business mailbox.

For doing this we need:

1. User Interface for Add-in which shows buttons for opening and closing the form.
2. Window form for adding notification message.

The user interfaces in Add-in is created by adding a Ribbon.

What Is the Ribbon System?

The Ribbon system was introduced by Microsoft in Outlook 2010 for replacing traditional menus. The Ribbon contains multiple tabs, each with several groups of commands. Tabs represent common tasks in Outlook, such as adding a contact, Composing an email, or scheduling a meeting.

Steps for creating user interface through Ribbon in Visual Studio 2017

1. Right-click on the Add-in project. A pop-up with different options should appear. Now, click on the Add option inside the popup and then click on the New Item option. Choose Ribbon (Visual Designer) from an open window and give a specific name for a ribbon in the Name text box and then click on the OK button.
Now add two buttons inside the group box of Ribbon. For this, select button control from the Office Ribbon Controls section in Toolbox on the left side of the window.
Rename these buttons by using the property window.
For the rename button, right-click on the button and choose the properties option from the pop-up. The property window is open on the right side of the project. Using property window you can change the look and feel of the button. For example, you can change the text of the button using the Label property. You can also change the id of a button since there is an id property in the property window. Or, you can show the image on the button using image property, etc.
By clicking on Show Form and Close Form Button on Ribbon, I want to show and close the form.
To do this, double click on these two buttons one by one. By clicking the click event of the button, it will automatically be created in .cs file of the ribbon, then write code for opening and closing the form.

Code shows as below:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Office.Tools.Ribbon;

namespace OutlookAddIn1

{

public partial class Ribbon1

{

private void Ribbon1_Load(object sender, RibbonUIEventArgs e)

{

}

private void btnShowForm_Click(object sender, RibbonControlEventArgs e)

{

Form1 frm = new Form1();

frm.ShowDialog();

}

private void btnCloseForm_Click(object sender, RibbonControlEventArgs e)

{

Form1 frm = new Form1();

frm.Close();

}

}

}

Now, the next step is creating a form.

Steps for creating a form in Add-in

1. Right-click on the project, and then click on the Add option. Next, click on the New Item option. Choose Window Form from the open window and give it a specific name in the Name text box. Lastly, click on the OK button.
I am adding a text box for entering a Notification Message and a button for saving the message in a .xml file which is present in my C:/ drive.
Now, double click on the save and close button for adding some functionality and write code for saving the text of the textbox in the .xml file.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace OutlookAddIn1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

DataTable dt = new DataTable();

dt.Columns.Add("Message_body");

DataRow rw = dt.NewRow();

rw[0] = this.txtbody.Text;

dt.Rows.Add(new object[]

{

this.txtbody.Text

});

dt.TableName = "Message Body";

dt.WriteXml(@"C:\MessageBody.xml");

base.Close();

}

}

}

Now I am writing code in Addin.cs file for sending a notification message to the user’s personal email id whenever a new email is coming in their business mailbox.

For this, open Addin.cs file and write code :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml.Linq;

using Outlook = Microsoft.Office.Interop.Outlook;

using Office = Microsoft.Office.Core;

using System.Data;

using System.IO;

using System.Xml;

namespace OutlookAddIn1

{

public partial class ThisAddIn

{

Outlook.NameSpace outlookNameSpace = null;

Outlook.MAPIFolder inbox = null;

Outlook.Items items = null;

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

try

{

outlookNameSpace = this.Application.GetNamespace("MAPI");

//Fetching Inbox folder of Outlook

inbox = outlookNameSpace.GetDefaultFolder(

Microsoft.Office.Interop.Outlook.

OlDefaultFolders.olFolderInbox);

items = inbox.Items;

//This event will be fired when a User receives an E-mail message.

items.ItemAdd +=

new Outlook.ItemsEvents_ItemAddEventHandler(Items_ItemAdd);

}

catch (Exception)

{

}

}

void Items_ItemAdd(object Item)

{

try

{

string strFrom = string.Empty;

string strTo = string.Empty;

string strBody = string.Empty;

string strSubject = string.Empty;

// gets all details of incoming mail

Outlook.MailItem mail = (Outlook.MailItem)Item;

// gets recepients name

Outlook.Recipients recipients = mail.Recipients;

List<string> lstRecipientList = new List<string>();

//stores list of recipients into a list

foreach (Outlook.Recipient recip in mail.Recipients)

{

if (recip.Resolve() == true)

{

if (recip.Type == (int)Outlook.OlMailRecipientType.olTo)

{

//checking for Exchange user

if (recip.AddressEntry.GetExchangeUser() != null)

{

lstRecipientList.Add(recip.AddressEntry.GetExchangeUser().PrimarySmtpAddress);

}

//checking for users other then Exchange user

else

{

lstRecipientList.Add(recip.Address);

}

}

}

}

for (int x = 0; x < lstRecipientList.Count; x++)

{

strFrom = string.Empty;

strTo = string.Empty;

strSubject = string.Empty;

strBody = GetMessageBody(); //Body of E-mail

strFrom = mail.SenderEmailAddress; //Sender email address

strTo = lstRecipientList[x].ToString(); //Recipient email address

strSubject = mail.Subject; // Subject of E-mail

SendEmail(strFrom, strTo, strBody, strSubject);

}

}

catch (Exception)

{

}

}

private string GetMessageBody()

{

DataSet ds = new DataSet();

string strFileURL = @"C:\MessageBody.xml";

if (File.Exists(strFileURL))

{

FileStream input = new FileStream(strFileURL, FileMode.Open);

XmlTextReader xmlTextReader = new XmlTextReader(input);

try

{

ds.ReadXml(xmlTextReader);

xmlTextReader.Close();

}

catch (Exception)

{

return null;

}

}

if (ds != null)

return ds.Tables[0].Rows[0][0].ToString();

else

return null;

}

private void SendEmail(string strFrom, string strTo, string strBody, string strSubject)

{

try

{

if (!string.IsNullOrEmpty(strFrom) && !string.IsNullOrEmpty(strTo))

{

Outlook.MailItem mailItem = (Outlook.MailItem)this.Application.CreateItem(Outlook.OlItemType.olMailItem);

mailItem.Subject = strSubject.Trim();//subject of mail

mailItem.To = “[email protected]”;//Personal email address of user

mailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;

mailItem.HTMLBody = strBody;//body of email

mailItem.Importance = Outlook.OlImportance.olImportanceLow;

mailItem.Display(false);

mailItem.Send();//send mail

}

}

catch (Exception)

{

}

}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)

{

}

#region VSTO generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InternalStartup()

{

this.Startup += new System.EventHandler(ThisAddIn_Startup);

this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);

}

#endregion

}

}

Now, Build your application by right click on the solution and then click on the build option.
Now add-in is ready for a run. For running add-in Press F5.

Sounds so simple? Isn’t it? So, simply follow and get started!

Follow me!
Latest posts by Imran Saifi (see all)

2 thoughts on “How to create VSTO Add-in for Outlook 2013 and 2016”

  1. Hi Asha,
    Thank you very much for you article.

    I have done the tutorial but when I run the solution : Outlook open but I don’t see the new Tab in the Ribbon with the button.
    Do you know how can add it?

    Thanks,

    Reply

Leave a Comment