In Technical Blogs
0
63 views

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 that program.

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.

2. From Add New Project Dialog Box, expand Visual C# option and then expand Office/SharePoint option and then select VSTO Add-ins option and then double click on the Outlook 2013 and 2016 VSTO Add-in option from right-hand side box.

Give a proper name for add-in in Name Textbox and then choose a location from where you want to keep your add-in application and then click on 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 which sends some notification message to user 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 Ribbon. 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 Add-in project, a pop up appears with different options. Now click on Add option inside the popup and then click on New Item option. Choose Ribbon(Visual Designer) from an open window and Give a specific name for a ribbon in Name text box and then click on OK button.

Design view of Ribbon is open as shown in below pic:

Now add two buttons inside group box of Ribbon. For this select button control from Office Ribbon Controls section in Toolbox on the left side of the window.

Rename these buttons by using property window.

For rename button, right click on the button and choose properties option from pop up. Property window is open on right side of the project. Using property window you can change look and feel of the button. For example, you can change the text of button using Label property, for changing the id of a button there is id property in 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.
For this double click on these two buttons one by one, By clicking, click event of the button will automatically be created in .cs file of ribbon, then write code for open 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 next step is creating form.

Steps for creating form in Add-in

1. Right-click on the project and then click on Add option and then click on New Item option. Choose Window Form from the open window and give a specific name for Form in Name text box and then click on OK button.

Design form according to your requirements

I am adding a text box for entering Notification Message and button for saving the message in a .xml file which is present in my c drive.

Now double click on save and close button for adding some functionality and write code for saving the text of 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 user 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 = “abc@gmail.com”;//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 solution and then click on 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!

RECOMMENDED POSTS
We Are Here to Help!
Name:
Phone:
Email:
Country:
Message:
 

Start typing and press Enter to search