繁体   English   中英

如何更改Outlook COM加载项中创建的约会的“发件人”?

[英]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个不同的帐户。 然后,如果我进入约会的“邀请与会者”,则“发件人”将始终显示我的两个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;
    }
}

但是,这只会使“邀请与会者”部分中的“发件人”字段为空白,而不是显示我为其设置的帐户。 我在这里做错了什么?

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

AppointmentItem.SendUsingAccount属性允许指定一个Account对象,该对象代表将在其下发送AppointmentItem的帐户。

更改会议主持人的AppointmentItem对象的属性是什么?

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

如何:创建新的Outlook约会项目文章介绍了在Outlook中创建约会项目的所有可能方法。 尝试获取正确的文件夹,并使用Items类的Add方法。 例如:

 items.Add(Outlook.OlItemType.olAppointmentItem)

Store类的GetDefaultFolder方法返回一个Folder对象,该对象表示商店中的默认文件夹,并且由FolderType参数指定类型。 此方法类似于NameSpace对象的GetDefaultFolder方法。 不同之处在于此方法在与帐户关联的传递存储上获取默认文件夹,而NameSpace.GetDefaultFolder返回当前配置文件在默认存储上的默认文件夹。

最后,我设法通过Application.ActiveExplorer().CurrentFolder查找当前选定的文件夹,然后通过.Store.GetDefaultFolder()来获取其默认日历文件夹,从而使此工作正常进行。 这使我可以通过正确的日历创建一个新的日历项目,为当前选择的文件夹自动设置适当的“发件人”地址。 这是使用的代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM