为什么我不能在 C# 中循环 Outlook.Selection

问题描述 投票:0回答:2

我有一个小程序,用于获取我邮箱中所有选定电子邮件的 entryId,但由于某种原因,我无法在不出现错误的情况下到达循环末尾。下面的程序旨在使用 OutLookApp.activeExplorer().Selection 并返回所有选定的 Mailitems,从那里我需要循环选择并获取每个 EntryId 并将它们添加到以逗号分隔的列表中,代码有时循环 2 或 4 次,但总是无法完成。我不太明白这个错误,希望有人能帮忙。

    private string GetEntryID(string ExecutionSource) 
    {
        string TempIDStorage           = null;
        Outlook.Application OutLookApp = _outlookManager.OutlookApplication;
        MailItem OutlookMItem          = null;
        Outlook.Selection _Selection   = null;
        Outlook.Inspector _Inspector   = null;

        if (ExecutionSource == "Main")
        {
            _Selection = OutLookApp.ActiveExplorer().Selection;              
            //Selection = commandContext.Context as Outlook.Selection;
            if (_Selection != null && _Selection.Count > 0)
            {               
                foreach (MailItem items in _Selection) 
                {                      
                    OutlookMItem  = items as Outlook.MailItem; 
                    TempIDStorage = OutlookMItem.EntryID + "," + TempIDStorage;
                }   
            }
        }
        else 
        {
            _Inspector = OutLookApp.ActiveInspector();
            if(_Inspector != null) 
            {
                OutlookMItem = _Inspector.CurrentItem as Outlook.MailItem;
                TempIDStorage = OutlookMItem.EntryID; 
            }
        }
        return TempIDStorage; 
    }
c# .net outlook com-interop office-automation
2个回答
1
投票

您假设选择仅包含

MailItem
对象。您还可以有
MeetingItem
ReportItem
对象:

foreach (object item in _Selection) 
{                      
    if (item is MailItem mi)
    {
        TempIDStorage = mi.EntryID + "," + TempIDStorage;
    }
}  

您可能还想考虑使用

StringBuilder
类而不是 TempIDStorage
string
变量。


0
投票

Outlook 文件夹包含不同类型的项目 - 约会、笔记、文档、邮件等。您需要在运行时检查项目类型,例如:

Outlook.Explorer currentExplorer = null;

private void ThisAddIn_Startup
    (object sender, System.EventArgs e)
{
    currentExplorer = this.Application.ActiveExplorer();
    currentExplorer.SelectionChange += new Outlook
        .ExplorerEvents_10_SelectionChangeEventHandler
        (CurrentExplorer_Event);
}

private void CurrentExplorer_Event()
{
    Outlook.MAPIFolder selectedFolder =
        this.Application.ActiveExplorer().CurrentFolder;
    String expMessage = "Your current folder is "
        + selectedFolder.Name + ".\n";
    String itemMessage = "Item is unknown.";
    try
    {
        if (this.Application.ActiveExplorer().Selection.Count > 0)
        {
            Object selObject = this.Application.ActiveExplorer().Selection[1];
            if (selObject is Outlook.MailItem)
            {
                Outlook.MailItem mailItem =
                    (selObject as Outlook.MailItem);
                itemMessage = "The item is an e-mail message." +
                    " The subject is " + mailItem.Subject + ".";
                mailItem.Display(false);
            }
            else if (selObject is Outlook.ContactItem)
            {
                Outlook.ContactItem contactItem =
                    (selObject as Outlook.ContactItem);
                itemMessage = "The item is a contact." +
                    " The full name is " + contactItem.Subject + ".";
                contactItem.Display(false);
            }
            else if (selObject is Outlook.AppointmentItem)
            {
                Outlook.AppointmentItem apptItem =
                    (selObject as Outlook.AppointmentItem);
                itemMessage = "The item is an appointment." +
                    " The subject is " + apptItem.Subject + ".";
            }
            else if (selObject is Outlook.TaskItem)
            {
                Outlook.TaskItem taskItem =
                    (selObject as Outlook.TaskItem);
                itemMessage = "The item is a task. The body is "
                    + taskItem.Body + ".";
            }
            else if (selObject is Outlook.MeetingItem)
            {
                Outlook.MeetingItem meetingItem =
                    (selObject as Outlook.MeetingItem);
                itemMessage = "The item is a meeting item. " +
                     "The subject is " + meetingItem.Subject + ".";
            }
        }
        expMessage = expMessage + itemMessage;
    }
    catch (Exception ex)
    {
        expMessage = ex.Message;
    }
    MessageBox.Show(expMessage);
}

有关详细信息,请参阅如何:以编程方式确定当前 Outlook 项目

在您的场景中,您需要使用一个对象遍历

Selection
集合中的所有项目:

foreach(object item in _Selection) 
{ 
   if (item is Outlook.MailItem)
   {
     Outlook.MailItem mailItem = (item as Outlook.MailItem);
     TempIDStorage = mailItem.EntryID + "," + TempIDStorage;
   }                
}   

此外,如果您需要获取像

EntryID
这样的属性值,您可以使用后期绑定技术,而无需在运行时检查项目类型。这是因为所有 Outlook 项目都提供
EntryID
属性,您可以认为它存在于 Outlook 中的每个选定项目上。 Type.InvokeMember 调用当前
Type
.

的特定成员
© www.soinside.com 2019 - 2024. All rights reserved.