2

我是 Outlook 插件开发的新手。我正在编写一个简单的应用程序,它打印出电子邮件被拖入的文件夹的名称。IE:收件箱到收件箱中的子文件夹。我遇到的问题是,有时会返回正确的 MailItem.Parent.Name,但大多数情况下它是源文件夹而不是目标文件夹。我不明白为什么这可能是因为应该为目标上的 ItemAdd 触发事件。

这是一些代码:

public Microsoft.Office.Interop.Outlook.Application OutlookApplication;
public Inspectors OutlookInspectors;
public Inspector OutlookInspector;
public MailItem OutlookMailItem;
private MAPIFolder inboxFolder;
private MailItem msg;
private Folder fdr;

public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
{
  OutlookApplication = application as Microsoft.Office.Interop.Outlook.Application;
  OutlookInspectors = OutlookApplication.Inspectors;
  OutlookInspectors.NewInspector += new Microsoft.Office.Interop.Outlook.InspectorsEvents_NewInspectorEventHandler(OutlookInspectors_NewInspector);

  inboxFolder = this.OutlookApplication.Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);

  foreach (Folder f in inboxFolder.Folders)
  {
    f.Items.ItemAdd += new ItemsEvents_ItemAddEventHandler(InboxItems_ItemAdd);
  }
}

void InboxItems_ItemAdd(object Item)
{
  msg = Item as MailItem;
  fdr = msg.Parent as Folder;

  MessageBox.Show("Folder Name: " + fdr.Name);
}
4

1 回答 1

0

我不知道你是如何让Items.ItemAdd事件发生的。我无法让您的示例正常工作,因此我创建了自己的示例。以下每次都对我有用,它总是显示目标文件夹名称。如果您不专门将集合保留Outlook.Items为类成员,则永远不会触发事件。请参阅相关的 SO 帖子

private Outlook.Folder inbox;
private List<Outlook.Items> folderItems = new List<Outlook.Items>();

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    inbox = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
    for (int i = 1; i < inbox.Folders.Count+1; i++)
    {
        folderItems.Add((inbox.Folders[i] as Outlook.Folder).Items);
        folderItems[i - 1].ItemAdd += (item) =>
        {
            Outlook.MailItem msg = item as Outlook.MailItem;
            Outlook.Folder target = msg.Parent as Outlook.Folder;
            string folderName = target.Name;
        };
    }
}
于 2012-09-06T15:13:44.007 回答