Search code examples
c#.netoutlookcomoffice-interop

How to change the "From" of a created appointment in Outlook COM add-in?


I'm writing an Outlook COM add-in in C#/.NET using Microsoft.Office.Interop.Outlook. I can create a new appointment item like so:

using Outlook = Microsoft.Office.Interop.Outlook;

[...]

var appointment = (Outlook.AppointmentItem)Globals.ThisAddIn.Application.CreateItem(Outlook.OlItemType.olAppointmentItem);
appointment.Display(true);

However, I have 2 different accounts set up in my Outlook. If I then go into the appointment's "Invite Attendees", the "From" always shows the first of my two Outlook accounts.

I tried setting the .SendUsingAccount property to the other account in my Outlook profile, which I got from the current session:

var accounts = Globals.ThisAddIn.Application.Session.Accounts;
foreach (Outlook.Account acct in accounts) {
    if (acct.DisplayName == "[desired account display name]") {
        appointment.SendUsingAccount = acct;
        break;
    }
}

However, this just makes the "From" field blank in the "Invite Attendees" section rather than showing the account I set it to. What am I doing wrong here?


Solution

  • In the end, I managed to get this to work by finding out the currently-selected folder via Application.ActiveExplorer().CurrentFolder and then getting its default calendar folder through .Store.GetDefaultFolder(). This allowed me to create a new calendar item through the correct calendar, automatically setting the appropriate "From" address for the currently-selected folder. Here's the code used:

    using Outlook = Microsoft.Office.Interop.Outlook;
    
    [...]
    
    Outlook.Explorer activeExplorer = null;
    Outlook.Store currentStore = null;
    Outlook.MAPIFolder calendarFolder = null;
    Outlook.Items items = null;
    Outlook.AppointmentItem appointment = null;
    try {
        // Get default calendar for currently-selected folder
        activeExplorer = Globals.ThisAddIn.Application.ActiveExplorer();
        if (activeExplorer == null) {
            throw new Exception("No active explorer.");
        }
        currentStore = activeExplorer?.CurrentFolder?.Store;
        if (currentStore == null) {
            throw new Exception("No current store.");
        }
        calendarFolder = currentStore?.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
        if (calendarFolder == null) {
            throw new Exception("No default calendar folder.");
        }
        items = calendarFolder?.Items;
        if (items == null) {
            throw new Exception("No calendar folder items.");
        }
    
        // Populate a new outlook appointment object with the appointment info
        appointment = items.Add(Outlook.OlItemType.olAppointmentItem) as Outlook.AppointmentItem;
    
        // Setup appointment
        [...]
    
        // Display the appointment window to the user
        appointment.Display(true);
    }
    catch (Exception ex) {
        MessageBox.Show(Resources.AppointmentError + ": " + ex.Message);
    }
    finally {
        if (activeExplorer != null) { Marshal.ReleaseComObject(activeExplorer); }
        if (currentStore != null) { Marshal.ReleaseComObject(currentStore); }
        if (calendarFolder != null) { Marshal.ReleaseComObject(calendarFolder); }
        if (items != null) { Marshal.ReleaseComObject(items); }
        if (appointment != null) { Marshal.ReleaseComObject(appointment); }
    }