TableView 对象的 GetTable 方法可以返回一个 Table 对象,该对象包含来自同一存储区中的一个或多个文件夹的项目,或者在聚合视图中跨多个存储区。 这在您希望访问从搜索(例如,搜索某个存储中的所有邮件项目)返回的项目时尤其有用。 本主题用一个示例来演示如何使用“即时搜索”搜索从当前用户的经理接收的标记为重要的所有项目,并显示每个搜索结果的主题。
下面的代码示例包含 GetItemsInView 方法。
GetItemsInView首先进行一些检查,查看 Outlook 会话的当前用户是否使用Microsoft Exchange Server、当前用户是否有经理,以及即时搜索是否在会话的默认存储中正常运行。
因为最终搜索依赖于 Explorer 对象的 Search 方法,并且最终结果显示使用 GetTable 方法,该方法基于活动资源管理器当前文件夹的当前视图,GetItemsInView创建一个资源管理器,显示此资源管理器中的收件箱,并使用此 Explorer 对象设置搜索。
GetItemsInView 将搜索条件指定为当前用户的经理中的重要项目,并将搜索范围指定为 MailItem 项目类型的所有文件夹。
调用 Explorer.Search 方法后GetItemsInView,任何搜索结果都将显示在此资源管理器中,包括来自其他文件夹和满足搜索条件的存储区的项目。
GetItemsInView 获取一个 TableView 对象,该对象包含搜索结果的此资源管理器视图。 通过使用此 TableView 对象的 GetTable 方法,GetItemsInView然后获取一个 Table 对象,该对象包含从搜索返回的聚合项。 最后 GetItemsInView ,显示 Table 对象的每一行的主题列,该行代表搜索结果中的一个项目。
下面的托管代码是使用 C# 编写的。 若要运行需要调入组件对象模型 (COM) 的 .NET Framework 托管代码示例,您必须使用可定义托管接口并将其映射到对象模型类型库中的 COM 对象的互操作程序集。 对于 Outlook,您可以使用 Visual Studio 和 Outlook 主互操作程序集 (PIA)。 在您运行适用于 Outlook 2013 的托管代码示例之前,请确保您已安装了 Outlook 2013 PIA 并且已添加了对 Visual Studio 中的 Microsoft Outlook 15.0 对象库组件的引用。
使用适用于 Visual Studio) 的 Office 开发人员工具 (Outlook 外接程序类中的以下代码 ThisAddIn 。 代码中的 应用程序对象必须是由 提供的受信任 Outlook ThisAddIn.Globals对象。 有关使用 Outlook PIA 开发托管 Outlook 解决方案的详细信息,请参阅 Outlook 主互作程序集参考。
private void GetItemsInView()
{
Outlook.AddressEntry currentUser =
Application.Session.CurrentUser.AddressEntry;
// Check if the current user uses the Exchange Server.
if (currentUser.Type == "EX")
{
Outlook.ExchangeUser manager =
currentUser.GetExchangeUser().GetExchangeUserManager();
// Check if the current user has a manager.
if (manager != null)
{
string managerName = manager.Name;
// Check if Instant Search is enabled and operational in the default store.
if (Application.Session.DefaultStore.IsInstantSearchEnabled)
{
Outlook.Folder inbox =
Application.Session.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderInbox);
// Create a new explorer to display the Inbox as
// the current folder.
Outlook.Explorer explorer =
Application.Explorers.Add(inbox,
Outlook.OlFolderDisplayMode.olFolderDisplayNormal);
// Make the new explorer visible.
explorer.Display;
// Search for items from the manager marked important,
// from all folders of the same item type as the current folder,
// which is the MailItem item type.
string searchFor =
"from:" + "\"" + managerName
+ "\"" + " importance:high";
explorer.Search(searchFor,
Outlook.OlSearchScope.olSearchScopeAllFolders);
// Any search results are displayed in that new explorer
// in an aggregated table view.
Outlook.TableView tableView =
explorer.CurrentView as Outlook.TableView;
// Use GetTable of that table view to obtain items in that
// aggregated view in a Table object.
Outlook.Table table = tableView.GetTable();
while (!table.EndOfTable)
{
// Then display each row in the Table object
// that represents an item in the search results.
Outlook.Row nextRow = table.GetNextRow();
Debug.WriteLine(nextRow["Subject"]);
}
}
}
}
}
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。