gpt4 book ai didi

c# - Outlook 插件 - Folder.ItemAdd 返回 parent.Name 作为源而不是目标

转载 作者:太空宇宙 更新时间:2023-11-03 16:26:56 25 4
gpt4 key购买 nike

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

最佳答案

我不知道您是如何触发 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;
};
}
}

关于c# - Outlook 插件 - Folder.ItemAdd 返回 parent.Name 作为源而不是目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12269020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com