如何更改 Outlook COM 加载项中已创建约会的 "From"?

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

我正在使用 Microsoft.Office.Interop.Outlook 在 C#/.NET 中编写 Outlook COM 加载项。我可以像这样创建一个新的约会项目:

using Outlook = Microsoft.Office.Interop.Outlook;

[...]

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

但是,我在 Outlook 中设置了 2 个不同的帐户。如果我随后进入约会的 "Invite Attendees","From" 总是显示我的两个 Outlook 帐户中的第一个。

我尝试将 .SendUsingAccount 属性 设置为我从当前会话获得的 Outlook 配置文件中的另一个帐户:

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

但是,这只会使 "Invite Attendees" 部分的 "From" 字段空白,而不是显示我设置的帐户。我在这里做错了什么?

尝试调用 Save 方法来应用通过 OOM 所做的更改。

AppointmentItem.SendUsingAccount 属性 允许指定一个帐户对象,该对象表示要发送 AppointmentItem 的帐户。

What is the property of AppointmentItem Object, that changes the meeting host?

最简单的方法是在属于特定帐户的日历文件夹中创建约会项目。您使用什么代码创建约会项目?

Itemsclass的How To: Create a new Outlook Appointment item article explains all possible ways for creating appointment items in Outlook. Try to get the right folder and use the Add方法。例如:

 items.Add(Outlook.OlItemType.olAppointmentItem)

Store GetDefaultFolder 方法 class returns Folder 对象表示存储中的默认文件夹,并且其类型由 FolderType 指定争论。此方法类似于 NameSpace 对象的 GetDefaultFolder 方法。不同之处在于此方法获取与帐户关联的交付商店上的默认文件夹,而 NameSpace.GetDefaultFolder returns 当前配置文件的默认商店上的默认文件夹。

最后,我通过 Application.ActiveExplorer().CurrentFolder 找到当前选择的文件夹,然后通过 .Store.GetDefaultFolder() 获取其默认日历文件夹,设法使它起作用。这使我能够通过正确的日历创建新的日历项目,自动为当前选定的文件夹设置适当的 "From" 地址。这是使用的代码:

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); }
}