Pieter van der Westhuizen

Sending and updating Outlook Calendar’s appointments and meeting requests

2015 is here, and from all of us at Add-in Express we wish you a very prosperous year. We’ll kick off this year by taking a closer look at Microsoft Outlook appointments and meetings.

Difference between Outlook appointments and meetings

In essence the Outlook Appointment and Meeting objects are the same thing. A meeting is an appointment scheduled by inviting attendees whereas an appointment is an entry on the users’ calendar that is set for a specific date and time.

The Outlook AppointmentItem can be a meeting, an event or recurring meetings or events. A meeting that is sent to other users arrives in their Inbox as an Outlook MeetingItem. If the user accepts the meeting request it is saved as an AppointmentItem in the users’ Outlook calendar.

The following examples were created using Add-in Express for Office and .NET. All of the code samples are written in C#, which is my language of choice, but you are free to use VB.NET or Visual C++.NET as well.

Creating an Outlook appointment programmatically

In the following code, we’ll create a basic appointment in our Outlook calendar by using the CreatItem method of the Outlook Application object.

private void adxCreateAppointmentRibbonButton_OnClick(object sender, IRibbonControl control, bool pressed)
    {
        Outlook.AppointmentItem appointment = null;
        try
        {
            appointment = (Outlook.AppointmentItem)OutlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
            appointment.Start = new DateTime(2015, 01, 25, 13, 30, 00);
            appointment.End = new DateTime(2015, 01, 25, 15, 30, 00);
            appointment.Subject = "Drink Coffee";
            appointment.Location = "Bob's Donut Shop";
            appointment.Body = "Go have some coffee and a donut at Bob's";
            appointment.BusyStatus = Outlook.OlBusyStatus.olOutOfOffice;
            appointment.Save();
        }
        finally
        {
            if (appointment != null) Marshal.ReleaseComObject(appointment);
        }
    }

Note that we set both the Start and End properties to DateTime object. You also have the option to only set the Start property and then specify the Duration property. The Duration property accepts an integer value which is the duration of the appointment in minutes.

In the following code, we’ll schedule the same appointment in Outlook, but this time we’ll set the Duration to 2 hours.

private void adxCreateAppointmentRibbonButton_OnClick(object sender, IRibbonControl control, bool pressed)
    {
        Outlook.AppointmentItem appointment = null;
        try
        {
            appointment = (Outlook.AppointmentItem)OutlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
            appointment.Start = new DateTime(2015, 01, 25, 13, 30, 00);
            appointment.Duration = 120;
            appointment.Subject = "Drink Coffee";
            appointment.Location = "Bob's Donut Shop";
            appointment.Body = "Go have some coffee and a donut at Bob's";
            appointment.BusyStatus = Outlook.OlBusyStatus.olOutOfOffice;
            appointment.Save();
        }
        finally
        {
            if (appointment != null) Marshal.ReleaseComObject(appointment);
        }
    }

Creating a reminder for an appointment in Outlook

In order to create a reminder for an Outlook appointment, we can create the appointment in a similar way as we’ve done previously and set the ReminderSet property to True. The ReminderMinutesBeforeStart is a Long value that sets the amount of minutes before the appointment to show the reminder.

In the following example, we’ll schedule a new appointment and set the Outlook calendar to remind us 30 minutes before the appointment.

private void adxAppointmentWithReminderRibbonButton_OnClick(object sender, IRibbonControl control, bool pressed)
    {
        Outlook.AppointmentItem appointment = null;
        try
        {
            appointment = (Outlook.AppointmentItem)OutlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
            appointment.Start = new DateTime(2015, 01, 20, 10, 00, 00);
            appointment.End = new DateTime(2015, 01, 20, 11, 00, 00);
            appointment.ReminderSet = true;
            appointment.ReminderMinutesBeforeStart = 30;
            appointment.Subject = "Dentist Visit";
            appointment.Location = "Medical Building, 123 West Street";
            appointment.Save();
        }
        finally
        {
            if (appointment != null) Marshal.ReleaseComObject(appointment);
        }
    }

Creating recurring appointments in Outlook calendar

Recurring appointments in Outlook are appointments that happen on a regular basis. In order to create a recurring Outlook appointment we need to use the RecurrencePattern property of the Appointment item. In the following code, we’ll create a new appointment that occurs every Tuesday and Friday.

private void adxRecurringAppointmentRibbonButton_OnClick(object sender, IRibbonControl control, bool pressed)
    {
        Outlook.AppointmentItem appointment = null;
        Outlook.RecurrencePattern recurrencePattern = null;
        try
        {
            appointment = (Outlook.AppointmentItem)OutlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
            recurrencePattern = appointment.GetRecurrencePattern();
            recurrencePattern.RecurrenceType = Outlook.OlRecurrenceType.olRecursWeekly;
            recurrencePattern.DayOfWeekMask = Outlook.OlDaysOfWeek.olTuesday | Outlook.OlDaysOfWeek.olFriday;
            recurrencePattern.PatternStartDate = new DateTime(2015, 01, 13);
            recurrencePattern.PatternEndDate = new DateTime(2015, 03, 20);
            recurrencePattern.StartTime = new DateTime(2015, 01, 13, 8, 0, 0);
            recurrencePattern.EndTime = new DateTime(2015, 03, 20, 8, 0, 0);
            recurrencePattern.Duration = 30;
            appointment.Subject = "Project stand up meeting";
            appointment.Save();
        }
        finally
        {
            if (appointment != null) Marshal.ReleaseComObject(appointment);
            if (recurrencePattern != null) Marshal.ReleaseComObject(recurrencePattern);
        }
    }

Creating and sending an Outlook meeting request

To create a new meeting request in the Outlook calendar, we’ll create a new appointment item and add recipients to the appointment items’ Recipients collection. Recipients are added using the Add method, which accepts a string parameter with the contact name to invite to the meeting.

If a recipient is added to the Recipients collection that does not exist in the Outlook address book an exception will be thrown. In order to avoid this you can use the Resolve method of the Recipient item or the ResolveAll method of the Recipients collection.

In the following code, we’ll create a new Outlook meeting request and invite one attendee after resolving their name.

private void adxCreateMeetingRibbonButton_OnClick(object sender, IRibbonControl control, bool pressed)
    {
        Outlook.AppointmentItem appointment = null;
        Outlook.Recipients recipients = null;
        Outlook.Recipient recipient = null;
 
        try
        {
            appointment = (Outlook.AppointmentItem)OutlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
            appointment.Subject = "A meeting";
            appointment.Start = DateTime.Now.AddHours(1);
            appointment.Duration = 60;
            appointment.MeetingStatus = Outlook.OlMeetingStatus.olMeeting;
            recipients = appointment.Recipients;
            recipient = recipients.Add("Alfred Smith");
            recipient.Type = (int)Outlook.OlMeetingRecipientType.olRequired;
            if (recipient.Resolve())
            appointment.Send();
        }
        finally
        {
            if (recipient != null) Marshal.ReleaseComObject(recipient);
            if (recipients != null) Marshal.ReleaseComObject(recipients);
            if (appointment != null) Marshal.ReleaseComObject(appointment);
        }
    }

Showing the Check named dialog to manually resolve recipients

If the recipient cannot be resolved using the Resolve method, you can use the SelectNamesDialog to manually resolve the recipient. In the following code, we’ll create a new Outlook appointment and add recipients. We’ll then loop through the recipients and if the recipient cannot be resolved we’ll show the SelectNames Dialog so that the user can manually choose the recipients.

private void adxCheckNamesDialogRibbonButton_OnClick(object sender, IRibbonControl control, bool pressed)
    {
        Outlook.NameSpace nameSpace = null;
        Outlook.AppointmentItem appointment = null;
        Outlook.Recipients recipients = null;
        Outlook.Recipient recipient1 = null;
        Outlook.Recipient recipient2 = null;
        Outlook.SelectNamesDialog selectDialog = null;
        Outlook.Recipients selectDialogRecipients = null;
 
        try
        {
            nameSpace = OutlookApp.Session;
            appointment = (Outlook.AppointmentItem)OutlookApp.CreateItem(Outlook.OlItemType.olAppointmentItem);
            appointment.Subject = "A meeting";
            appointment.Start = DateTime.Now.AddHours(1);
            appointment.Duration = 60;
            appointment.MeetingStatus = Outlook.OlMeetingStatus.olMeeting;
            recipients = appointment.Recipients;
 
            recipient1 = recipients.Add("Alfred Smith");
            recipient1.Type = (int)Outlook.OlMeetingRecipientType.olRequired;
 
            recipient2 = recipients.Add("Unknown Contact");
            recipient2.Type = (int)Outlook.OlMeetingRecipientType.olOptional;
 
            for (int i = 1; i <= recipients.Count; i++)
            {
                Outlook.Recipient recipient = recipients[i];
                if (!recipient.Resolve())
                {
                    selectDialog = nameSpace.GetSelectNamesDialog();
                    selectDialogRecipients = selectDialog.Recipients;
 
                    recipients.Remove(i);
                    selectDialog.Display();
                    if (selectDialogRecipients.ResolveAll())
                    {
                        recipients.Add(recipient.Name);
                    }
                    else
                    {
                        recipients.Remove(i);
                    }
 
                }
                if (recipient != null) Marshal.ReleaseComObject(recipient);
            }
        }
        finally
        {
            if (selectDialogRecipients != null) Marshal.ReleaseComObject(selectDialogRecipients);
            if (selectDialog != null) Marshal.ReleaseComObject(selectDialog);
            if (recipient2 != null) Marshal.ReleaseComObject(recipient2);
            if (recipient1 != null) Marshal.ReleaseComObject(recipient1);
            if (recipients != null) Marshal.ReleaseComObject(recipients);
            if (appointment != null) Marshal.ReleaseComObject(appointment);
            if (nameSpace != null) Marshal.ReleaseComObject(nameSpace);
        }
    }

Responding to an Outlook meeting request programmatically

To programmatically respond to a meeting request (i.e. accept or decline it), you will first need to check if the item is an Outlook MeetingItem. You can then use the GetAssociatedAppointment method to get the Outlook AppointmentItem that is associated with the meeting request and respond to the request by invoking the Respond method.

In the following code, we'll loop through the current selection in the Outlook Explorer and will check if the items are Outlook MeetingItems. If the item is a MeetingItem, we'll get the AppointmentItem that is associated with the meeting and accept it using the Resolve method.

private void adxAcceptMeetingRibbonButton_OnClick(object sender, IRibbonControl control, bool pressed)
    {
        Outlook.Explorer currExplorer = null;
        Outlook.Selection selection = null;
 
        try
        {
            currExplorer = OutlookApp.ActiveExplorer();
            selection = currExplorer.Selection;
 
            for (int i = 1; i <= selection.Count; i++)
            {
                if (selection[i] is Outlook.MeetingItem)
                {
                    Outlook.MeetingItem meeting = selection[i] as Outlook.MeetingItem;
                    Outlook.AppointmentItem appointment = meeting.GetAssociatedAppointment(true);
                    if (appointment != null)
                    {
                        appointment.Respond(Outlook.OlMeetingResponse.olMeetingAccepted, true);
                        Marshal.ReleaseComObject(appointment);
                    }
                    Marshal.ReleaseComObject(meeting);
                }
            }
        }
        finally
        {
            if (selection != null) Marshal.ReleaseComObject(selection);
            if (currExplorer != null) Marshal.ReleaseComObject(currExplorer);
        }
    }

Thank you for reading. Until next time, keep coding!

Available downloads:

This sample Outlook add-in was developed using Add-in Express for Office and .net:

Outlook Appointments sample add-in (C#)

27 Comments

  • SharePoint Developer says:

    Hi; I was trying to create a meeting request as shown in the code snippets. Is “OutlookApp” a custom class that you’ve created. I couldnt find it under the Microsoft.Office.interop.Outlook namespace. Thanks.

  • Pieter van der Westhuizen says:

    Hi There,

    OutlookApp is a property that is automatically added to the AddinModule class when creating an Outlook Add-in using Add-in Express.

    It looks like the following:

    public Outlook._Application OutlookApp
    {
    get
    {
    return (HostApplication as Outlook._Application);
    }
    }

    Hope this helps!

  • SharePoint Developer says:

    Thanks for the reply Pieter

  • Sergio says:

    It doesnt work in a server, works locally but when you publish that not work in a server… what its the best way to do that?

  • Pieter van der Westhuizen says:

    Hi Sergio,

    I’m afraid, I’m not sure what you mean. The code in this article is for a managed Outlook Add-in. It will be deployed using either clickonce or an msi installer on the users’ local machine.

  • Beginner says:

    Hi, I am trying to schedule meeting and send to myself through QTP, please help

  • Pieter van der Westhuizen says:

    Hi there,

    If by QTP you mean Quick Test Pro, then as far as I remember with QTP you can write VBA code.
    Try using the sample code as provided on this page in your VBA, you should be able to create and send Outlook appointments.

    You would need to reference the Microsoft Outlook Library in your VBA project for this to work.

  • Andre says:

    Hi

    I wrote a VB code in Developer as my company that searches all the boardrooms we have and returns all the available boardrooms in a listbox by using the getfreebusy function. It was too difficult to distribute the function so I rather want to create an add-in using c# for outlook to do the same but I don’t know how to code it.

    Can you maybe help

  • Pieter van der Westhuizen says:

    Hi Andre,

    It looks like you’re located in my neck of the woods. I’ve sent you an email. Maybe I can help you out.

  • Edwin says:

    Hi,

    I was wondering if you can solve my question. Can this work fine on a Web application, I mean, like a Intranet site?. I saw above that someone posted that this don’t work in a server

    Hope you can understand what I mean.

  • Dmitry Kostochko (Add-in Express Team) says:

    Hi Edwin,

    This article and its code are purposed for desktop Outlook versions only.

  • Imran says:

    Hi,

    Please help me as I want to develop web application in .net for Online meeting room book, pls find below as per my need.

    1. user entered all details as like mail id, subject, start dt, end dt, date, body etc..
    2. when user send meeting request, recipient receive mail notification and block outlook calendar for meeting request.
    3. same mail request send to recipient if booked user cancelled meeting.
    pls suggest me asap.

  • Andrei Smolin (Add-in Express Team) says:

    Hello Imran,

    Web applications are out of scope here. We discuss Office add-ins, not web applications. Specifically, the article above is devoted to Outlook add-ins.

  • Jaya Rayasam says:

    Hello,

    Is there a way I can schedule an online meeting or Skype Meeting in the outlook instead of normal meeting ?

  • Andrei Smolin (Add-in Express Team) says:

    Hello Jaya,

    The Outlook object model doesn’t provide such a way.

  • hammad says:

    great article

  • Hazie says:

    What if the location is an office room? assuming the room has an email address.

  • Shana says:

    Hello Andrei,

    I get “no source” error for get recipient method.

  • Andrei Smolin (Add-in Express Team) says:

    Hello Shana,

    The sample add-in discussed here doesn’t seem to provide such a method.

  • Linda says:

    Hello,
    I would like to automate the process of importing a csv file into Outlook calendar base on the active directory (mapping each person’s ID) . This would occur daily.
    What I want to do is simple but after some research I did not find anything helpful.

    Is there a way to completely automate the process of importing the csv file into the outlook calendar?

    Thank you very much!

    Linda

  • Andrei Smolin (Add-in Express Team) says:

    Hello Linda,

    If you use a COM add-in, you can provide a Ribbon button that the user presses to initiate the import. Alternatively, you can start the import when a given time moment occurs. While reading the CSV you are expected to create Appointment objects, set their properties and save the objects.

  • Bo says:

    Hi Expert,

    I hope to update the content of Meeting Invite body after I clicked send button (to meet some security check).

    I can update the meetingItem.Body property and it works perfectly, but I need to use Inspector.WordEditor property to insert HTML format data.

    When I try to get the inspector by using meetingItem.GetInspector, COM exception will throw.

    Could you please advise how to insert the HTML format data into the Meeting item that will be received for the recipients? From sender side there is no issue and appointment item’s inspector can be used to get the word editor.

  • Dmitry Kostochko (Add-in Express Team) says:

    Hello Bo,

    >> When I try to get the inspector by using meetingItem.GetInspector, COM exception will throw.

    What exactly exception do you see?

    >> Could you please advise how to insert the HTML format data into the Meeting item that will be received for the recipients?

    It is possible only via the Word Object Model because neither AppointmentItem nor MeetingItem provides the HTMLBody property.

  • Goddy says:

    Hello Pieter,

    your tutorial is so helpful to me especially as I am doing something similar at the moment.

    With regards to “Creating an Outlook appointment programmatically”, I did exactly as you and all is fine but at the level of “appointment.Location”, I will like to define several locations (like 3 to 4) so that the user can have the opportunity to select a particular location. So I thought of ComboxBox, but after alot of reasearch I have not been able to advance in this direction.

    I will be very grateful if you can help.

    Kind regards and nice weekend

  • Andrei Smolin (Add-in Express Team) says:

    Hello Goddy,

    If you talk about the UI that the meeting inspector provides, you can’t replace the built-in text box with a custom combo box. Consider showing a pane suggesting available locations. Note that the use may switch to the Scheduling Assistant tab and click Add Rooms; if your Exchange (or is it Active Directory?) is set up correctly, the user will be able to specify the location in a more systematic way.

  • Andrei Smolin (Add-in Express Team) says:

    Ah, sorry for a typo: Please replace “the use” with “the user”.

Post a comment

Have any questions? Ask us right now!