Pages

Friday, March 9, 2012

Outlook 2010 AddIn - Create new meeting request and mails programmatically

Few days back , questioned was thrown at me like - hey dude , have you ever tried extending ribbon in outlook? and my answer at that point was 'No'. then next question was - is it possible to have some buttons in the ribbon which will allow you to create new meeting request and mails with predefined data and recipients?

Question did sound interesting to me , and that immediately taken me in front of machine. I opened up VS 2010 to see what can be done.
It is easy to extend ribbon of Office applications using VS 2010 , as it gives you the project template.
I will not cover up how to create new ribbon and add buttons on it but here is what I learned on that evening - how to create new meeting request and new mails with predefined contents.
Just added two buttons on my custom ribbon , and added code behind of custom ribbon and buttons


private Application m_outlookApp;
private Explorer m_outlookActiveWindow;

  • To create new mail programmatically
 
try
{
    m_outlookApp = Globals.ThisAddIn.Application;
    m_outlookActiveWindow = m_outlookApp.ActiveExplorer();

    MailItem mailItem = m_outlookApp.CreateItem(OlItemType.olMailItem) as MailItem;

    if (mailItem != null)
    {
       mailItem.Body = "Add Your Contents Here..";
       mailItem.Subject = "Add Appointment Subject Here..";
       mailItem.To = "target.user@smtp.com";
       mailItem.Save();
       mailItem.Display(true);
       Marshal.ReleaseComObject(mailItem);
    }
 }
 catch (System.Exception ex)
 {
       System.Windows.Forms.MessageBox.Show("Error: " + ex.Message);
 }

  • To create new meeting request programmatically


try
{
   m_outlookApp = Globals.ThisAddIn.Application;
   m_outlookActiveWindow = m_outlookApp.ActiveExplorer();

   AppointmentItem appItem = m_outlookApp.CreateItem(OlItemType.olAppointmentItem) as AppointmentItem;

   if (appItem != null)
   {
     appItem.Body = "Add Your Contents Here..";
     appItem.Subject = "Add Appointment Subject Here..";
     appItem.RequiredAttendees = "target.user1@smtp.com;target.user2@smtp.com";
     appItem.OptionalAttendees = "optional.user1@smtp.com";
     appItem.Start = DateTime.Now.AddHours(1);
     appItem.End = DateTime.Now.AddHours(2);
     appItem.Save();
     appItem.Display(true);
     Marshal.ReleaseComObject(appItem);
   }
 }
 catch (System.Exception ex)
 {
     System.Windows.Forms.MessageBox.Show("Error: " + ex.Message);
 }


References:
http://blogs.msdn.com/b/mcsuksoldev/archive/2010/10/01/building-and-deploying-an-outlook-2010-add-in-part-1-of-2.aspx
http://www.add-in-express.com/creating-addins-blog/2011/11/04/outlook-create-appointment-item/
http://msdn.microsoft.com/en-us/library/bb612741.aspx


3 comments:

  1. Hi Bhushan,

    Excellent one. I need your help,

    I have the button in sharepoint webpart, If I click that button then one event will be created in Outlook 2010.

    Can you please provide your inputs.

    Thanks
    Murali Arumugam

    ReplyDelete
  2. Hey glad that post helped you.
    about your scenario - you should be able to do something like this - create new appointment on click of a button in your web part which will ultimately create an calendar item in outlook.

    you will need to add the reference of the Microsoft.Office.Interop.Outlook to your solution

    Example

    using Outlook = Microsoft.Office.Interop.Outlook;

    protected void Button1_Click(object sender, EventArgs e)
    {

    Outlook.Application Outlookapplication = null;

    try
    {
    Outlookapplication = GetOutlook();
    if (Outlookapplication != null)
    {
    AppointmentItem appItem = Outlookapplication.CreateItem(OlItemType.olAppointmentItem) as AppointmentItem;
    if (appItem != null)
    {
    appItem.Body = "Add Your Contents Here..";
    appItem.Subject = "Add Appointment Subject Here..";
    appItem.RequiredAttendees = "target.user1@smtp.com;target.user2@smtp.com";
    appItem.OptionalAttendees = "optional.user1@smtp.com";
    appItem.Start = DateTime.Now.AddHours(1);
    appItem.End = DateTime.Now.AddHours(2);
    appItem.Save();
    appItem.Display(true);
    Marshal.ReleaseComObject(appItem);
    }
    }

    }
    catch (System.Exception ex)
    {
    this.Controls.Add(new LiteralControl("Error: " + ex.Message));
    }
    }

    public Outlook.Application GetOutlook()
    {
    Outlook.Application application = null;
    // Check whether there is an Outlook process running.
    if (System.Diagnostics.Process.GetProcessesByName("OUTLOOK").Length > 0)
    {
    // If so, use the GetActiveObject method to obtain the process and cast it to an Application object.
    application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
    }
    else
    {

    // If not, create a new instance of Outlook and log on to the default profile.
    application = new Outlook.Application();
    Outlook.NameSpace nameSpace = application.GetNamespace("MAPI");
    nameSpace.Logon("", "", Missing.Value, Missing.Value);
    nameSpace = null;
    }

    return application;
    }

    Reference - http://msdn.microsoft.com/en-us/library/ff462097.aspx

    Hope this helps you.

    ReplyDelete
    Replies
    1. Hi Bhushan,

      First I want to say sorry for my late reply.
      I thanks allllllllllooooooooooottttttt for your great reply.
      The above code worked like as a charm. You are saved my time dear. Thank you again.

      Thanks
      Murali Arumugam

      Delete