Visual Studio Add-in and its power on Outlook programming
This article tells you how VS Add-in helps in Outlook programming.Instead of writing the classical macro, revealing the power of Visual Studio Add-in. To demonstrate the power of Add-in the program will check the subject line of a mail item if it is blank.
Do you use outlook in your organization? This article tells you how VS Add-in works with Outlook.
To demonstrate the fact I have build an add-in program which outlook loads during it starts up and the add-in will check if the subject of the mail item is blank or not? If it is blank, it will ask to the user whether the mail item sends without subject line or not?
To implement the solution I have created VS ->Office->Outlook 2007 Add-in Project.
Please find the following code in ThisAddIn.cs file.
public partial class ThisAddIn
{
MailItem _mailItem = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.Application.ItemSend += new
ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
void Application_ItemSend(object Item, ref bool Cancel)
{
if (Item is MailItem)
{
try
{
MailItem mailItem = Item as MailItem;
if (string.IsNullOrEmpty(mailItem.Subject))
{
DialogResult dialogResult = MessageBox.Show("Blank Subject. Do you want to continue?", "Notfify Window", MessageBoxButtons.YesNo,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button2);
if (dialogResult == DialogResult.Yes)
{
Cancel = false;
}
else if (dialogResult == DialogResult.No)
{
Cancel = true;
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString(),
"Add-in failed",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
//
}
#region VSTO generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
Build the solution and press F5.
Outlook will open and if you want to see the Add-in loaded successfully or not Go to Tools -> Trust Center ->Add-ins.
Now open the new email item and try to send an email without subject line to see the alert message is coming properly or not.
For Deployment:
1) Make a Setup of the project.
2) VSTO 3.0 Run time is required.
P.S: All the development performed on:
1) Development Performed on VS 2008.
2) MS Outlook 2007 Version used.
Regards,
Sumit