使用 Application 对象的 CreateItem 方法创建Microsoft Outlook 项目时,会为该会话的主帐户创建该项目。 在会话中,如果配置文件中定义了多个帐户,则可以针对特定的 IMAP、POP 或 Microsoft Exchange 帐户创建项目。 如果当前配置文件中存在多个帐户且你在用户界面中创建了可发送项目,例如,通过单击“新建电子邮件”或“新建会议”,检查器会显示撰写模式的新邮件项目或会议请求,那么你便可以选择要用于发送该项目的帐户。 本主题介绍如何以编程方式创建可发送项目和使用特定发送帐户发送该项目。 本主题包含两个代码示例,演示如何为活动资源管理器中的当前文件夹确定的特定帐户创建 MailItem 和 AppointmentItem 。
下面的托管代码是使用 C# 编写的。 若要运行需要调入组件对象模型 (COM) 的 .NET Framework 托管代码示例,您必须使用可定义托管接口并将其映射到对象模型类型库中的 COM 对象的互操作程序集。 对于 Outlook,您可以使用 Visual Studio 和 Outlook 主互操作程序集 (PIA)。 在您运行适用于 Outlook 2013 的托管代码示例之前,请确保您已安装了 Outlook 2013 PIA 并且已添加了对 Visual Studio 中的 Microsoft Outlook 15.0 对象库组件的引用。 应使用 Office Developer Tools for Visual Studio) 在 Outlook 外接程序 (类中使用以下代码 ThisAddIn 。 代码中的 应用程序对象必须是由 提供的受信任 Outlook ThisAddIn.Globals对象。 有关使用 Outlook PIA 开发托管 Outlook 解决方案的详细信息,请参阅欢迎使用 MSDN 上的 Outlook 主互作程序集参考 。
下面的 CreateMailItemFromAccount第一种方法为特定帐户创建 MailItem 并将其显示在撰写模式下;特定帐户的默认传递存储与活动资源管理器中显示的文件夹的存储相同。 该帐户的当前用户设置为发件人。
CreateMailItemFromAccount首先,通过将从 Folder.Store 属性) 获取的当前文件夹 (存储与使用会话的 Accounts 集合中定义的 Account.DeliveryStore 属性) 获取的每个帐户的默认传递存储 (匹配来标识相应的帐户。
CreateMailItemFromAccount 然后创建 MailItem。 若要将项目与帐户关联,CreateMailItemFromAccount请将帐户用户的 AddressEntry 对象设置为 MailItem 的 Sender 属性,将帐户的用户分配为项目的发件人。 指定 Sender 属性是非常重要的步骤,否则该 MailItem 是针对主帐户创建的。 在 方法的末尾, CreateMailItemFromAccount 显示 MailItem。 请注意,如果当前文件夹不在传递存储中, CreateMailItemFromAccount 只需为会话的主帐户创建 MailItem 即可。
private void CreateMailItemFromAccount()
{
Outlook.AddressEntry addrEntry = null;
// Get the store for the current folder.
Outlook.Folder folder =
Application.ActiveExplorer().CurrentFolder
as Outlook.Folder;
Outlook.Store store = folder.Store;
Outlook.Accounts accounts =
Application.Session.Accounts;
// Match the delivery store of each account with the
// store for the current folder.
foreach (Outlook.Account account in accounts)
{
if (account.DeliveryStore.StoreID ==
store.StoreID)
{
addrEntry =
account.CurrentUser.AddressEntry;
break;
}
}
// Create MailItem. Account is either the primary
// account or the account with a delivery store
// that matches the store for the current folder.
Outlook.MailItem mail =
Application.CreateItem(
Outlook.OlItemType.olMailItem)
as Outlook.MailItem;
if (addrEntry != null)
{
//Set Sender property.
mail.Sender = addrEntry;
mail.Display(false);
}
}
下一种方法CreateMeetingRequestFromAccount与 类似CreateMailItemFromAccount,只不过它创建 AppointmentItem 而不是 MailItem,并使用其 SendUsingAccount 属性将 AppointmentItem 与帐户相关联。
CreateMeetingRequestFromAccount 在帐户的“日历”文件夹中创建 AppointmentItem ,该帐户的默认传递存储区与活动资源管理器中显示的文件夹的存储相同。
CreateMeetingRequestFromAccount首先,通过将从 Folder.Store 属性) 获取的当前文件夹 (存储与使用会话的 Accounts 集合中定义的 Account.DeliveryStore 属性) 获取的每个帐户的默认传递存储 (匹配来标识相应的帐户。 然后, CreateMeetingRequestFromAccount 创建该 AppointmentItem 。 若要将项目与帐户关联,CreateMeetingRequestFromAccount请将 Account 对象设置为 AppointmentItem 的 SendUsingAccount 属性,将该帐户分配为项目的发送帐户。 指定 SendUsingAccount 属性是非常重要的步骤,否则该 AppointmentItem 是针对主帐户创建的。 在该方法的末尾, CreateMeetingRequestFromAccount 显示该 AppointmentItem 。 请注意,如果当前文件夹不在传递存储区上,则 CreateMeetingRequestFromAccount 只是针对该会话的主帐户创建该 AppointmentItem 。
private void CreateMeetingRequestFromAccount()
{
Outlook.Account acct = null;
// Get the store for the current folder.
Outlook.Folder folder =
Application.ActiveExplorer().CurrentFolder
as Outlook.Folder;
Outlook.Store store = folder.Store;
Outlook.Accounts accounts =
Application.Session.Accounts;
// Match the delivery store of each account with the
// store for the current folder.
foreach (Outlook.Account account in accounts)
{
if (account.DeliveryStore.StoreID ==
store.StoreID)
{
acct = account;
break;
}
}
// Create AppointmentItem. Account is either the primary
// account or the account with a delivery store
// that matches the store for the current folder.
Outlook.AppointmentItem appt =
Application.CreateItem(
Outlook.OlItemType.olAppointmentItem)
as Outlook.AppointmentItem;
appt.MeetingStatus =
Outlook.OlMeetingStatus.olMeeting;
if (acct != null)
{
//Set SendUsingAccount property.
appt.SendUsingAccount=acct;
appt.Display(false);
}
}
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。