C# 如何从Outlook.Application.ActiveExplorer.CurrentFolder创建Outlook.Folder对象

C# 如何从Outlook.Application.ActiveExplorer.CurrentFolder创建Outlook.Folder对象,c#,outlook,vsto,mapi,C#,Outlook,Vsto,Mapi,我有一个“简单”的Outlook对象: Outlook.Explorer olExplorer = this.Application.ActiveExplorer(); 在“ThisAddin_StartUp”中,我将olExplorer.FolderSwitch事件注册到函数olExplorer_FolderSwitch()。在此,我必须从当前文件夹中创建Outlook文件夹对象: Outlook.Folder f = olExplorer.CurrentFolder as Outlook.

我有一个“简单”的Outlook对象:

Outlook.Explorer olExplorer = this.Application.ActiveExplorer();
在“ThisAddin_StartUp”中,我将olExplorer.FolderSwitch事件注册到函数olExplorer_FolderSwitch()。在此,我必须从当前文件夹中创建Outlook文件夹对象:

Outlook.Folder f = olExplorer.CurrentFolder as Outlook.Folder;
但是:属性“CurrentFolder”的类型为MAPIFolder,不能用作Outlook.Folder。
如何将CurrentFolder属性“强制转换”到Outlook.Folder不丢失事件处理程序?如果我执行此简单转换,对象f将不会触发BeforeItemMove之前的事件-因为在olExplorer.CurrentFolder不为

的情况下,f为NULL,我并不真正了解问题,因为根据文档管理器。CurrentFolder返回Outlook.Folder类型的对象,而不是MAPIFolder类型的对象。我个人还没有做过任何VSTO(或针对2007的)开发,但是你确定没有混淆不同版本的对象模型吗


无论如何,Outlook.Folder和MAPIFolder共享EntryID和StoreID属性。您可以使用这些来查找相应的Outlook.Folder。所讨论的名称空间是通过Application.GetNamespace(“MAPI”)获取的

我没有找到一个简单的方法。 您可以从文件夹会话中找到Outlook.Folder

如果比较EntryID,您将得到正确的文件夹

Outlook.Folders olFolders = OutlookApp.Session.Folders;

for (int i = 1; i <= olFolders.Count; i++)
{
   if (olFolders[i].EntryID == olExplorer.CurrentFolder.EntryID)
   {
      // folder found assign and use it.
   }
}
Outlook.Folders olFolders=OutlookApp.Session.Folders;

对于(int i=1;i有一种将MAPIFolder转换为Outlook的简便方法。请尝试显式转换文件夹:

Outlook.Explorer olExplorer = this.Application.ActiveExplorer();
Outlook.Folder f = (Outlook.Folder)olExplorer.CurrentFolder;

Outlook.Folder与Outlook.MAPIFolder相同。

Explorer.CurrentFolder返回MAPIFolder类型的对象。