2012-09-04 30 views
2

我是Outlook Addin开发新手。我正在编写一个简单的应用程序,打印出电子邮件被拖入的文件夹的名称。 IE:收件箱中收件箱到子文件夹。我遇到的问题是,有时会返回正确的MailItem.Parent.Name,但大部分时间是源文件夹而不是目标。我不明白为什么这可能是因为该事件应该为目标上的ItemAdd点燃。Outlook Addin - Folder.ItemAdd返回parent.Name作为源不是目的地

下面是一些代码:

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

回答

0

我不知道你怎么连得Items.ItemAdd事件触发。我无法让你的例子工作,所以我创建了自己的例子。以下每次都为我工作,它始终显示目标文件夹名称。如果您没有专门将Outlook.Items作为集体成员保存,则事件永远不会被触发。见related SO post

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