tfl
tfl

Reputation: 1011

howto create an Outlook.Folder Object from Outlook.Application.ActiveExplorer.CurrentFolder

i have this "simple" Outlook-Object:

Outlook.Explorer olExplorer = this.Application.ActiveExplorer();

in "ThisAddin_StartUp" i register the olExplorer.FolderSwitch event to a function olExplorer_FolderSwitch(). There i must create an Outlook-Folder Object from the current Folder:

Outlook.Folder f = olExplorer.CurrentFolder as Outlook.Folder;

But: the property "CurrentFolder" is of type MAPIFolder and cant be used as Outlook.Folder. How can i "cast" the CurrentFolder-Property to an Outlook.Folder? - without loosing the event-handler? If i do this simple conversion, the object f will not fire the event BeforeItemMove - because f is NULL where olExplorer.CurrentFolder is not

Upvotes: 1

Views: 3064

Answers (4)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66341

Outlook.Folder is the same as Outlook.MAPIFolder.

Upvotes: 0

Vasyl
Vasyl

Reputation: 1423

There are easy way convert MAPIFolder to Outlook.Folder try explicit cast:

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

Upvotes: 1

Khan
Khan

Reputation: 516

I have not found an easy way. You could find the Outlook.Folder from the folders sessions.

If you compare the EntryID you will get the right folder.

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

notice the start at 1 and count equal or less to get all your folders.

Upvotes: 1

Paul-Jan
Paul-Jan

Reputation: 17278

I don't really get the issue, as according to the documentation Explorer.CurrentFolder returns an object of type Outlook.Folder, not MAPIFolder. I personally haven't done any VSTO (nor 2007 specific) development, but are you sure you aren't mixing up different versions of the object model?

Anyway, Outlook.Folder and MAPIFolder share the EntryID and StoreID property. You can use those to lookup the corresponding Outlook.Folder using NameSpace.GetFolderFromID. The namespace in question is acquired through Application.GetNamespace("MAPI").

Upvotes: 0

Related Questions