1

I can get default folders from outlook no problem, but I'm struggling to get custom folders. I want to get the emails from a folder in my outlook called "Mass Archive" but I am struggling to understand how to use:

.GetFolderFromID()

From what I gathered, the first parameter it takes is the name of the folder e.g. .GetFolderFromID("Mass Archive")

But I cannot figure out what I am supposed to put as the object for the second parameter.

I'm really newbie so please explain things to me like I'm dumb.

outlookApplication = new Application();
outlookNameSpace = outlookApplication.GetNamespace("MAPI");
//inboxFolder = outlookNameSpace.GetDefaultFolder(OlDefaultFolders.olFolderDeletedItems);
inboxFolder = outlookNameSpace.GetFolderFromID("Mass Archive", "Mass Archive");
mailItems = inboxFolder.Items;

foreach (MailItem item in mailItems)
{
    emailDetails = new OutlookEmails
    {
        EmailFrom = item.SenderEmailAddress,
        EmailSubject = item.Subject,
        EmailBody = item.Body,
        ReceivedTime = item.ReceivedTime
    };
    listEmailDetails.Add(emailDetails);
    ReleaseComObject(item);
}
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
David Andrew Thorpe
  • 838
  • 1
  • 9
  • 23
  • 1
    You've got code there to get a default folder - you could see what the StoreID for that looks like? I'd guess a GUID. But I don't think folder names are unique: you might have to recursively search through the tree if all you have is a folder name. – Rup Feb 06 '19 at 10:57
  • Have you looked at https://learn.microsoft.com/en-us/office/vba/api/outlook.namespace.getfolderfromid – Daniel Feb 06 '19 at 10:57
  • I have indeed but I'm a noob, and that's why I'm here asking a question because I still don't understand – David Andrew Thorpe Feb 06 '19 at 11:00
  • 1
    OK, it looks like these are all long hex strings. The StoreID I'd guess is an ID for the MailStore, i.e. the inbox or PST that you're working in, and is optional here. The EntryID is also a long hex string and I'd guess is a unique ID for that folder, at least unique within the store but likely across all open stores, so if you only need this one folder in the same inbox you can likely save this string ID and just use that with GetFolderFromID. (But if e.g. someone renames the folder and makes a new 'Mass Archive' the ID would still point to the old folder.) – Rup Feb 06 '19 at 11:41
  • @Rup thanks for your help so far, yes I printed out the entry/store ID's for the default folder and I see they are some very long strings! How am I supposed to get the EntryID for the folder that I want initially? – David Andrew Thorpe Feb 06 '19 at 12:44
  • 1
    There's two answers here that recurse through folders: https://stackoverflow.com/a/10733352/243245 (one in the MSDN link) You could do that and print the IDs as well as the names, or you could adapt one of those to search for the name in the first place. – Rup Feb 06 '19 at 12:52
  • Perfect, my question is answered. thanks @Rup – David Andrew Thorpe Feb 06 '19 at 13:36

2 Answers2

2

You don't need to search - you can open it using MAPIFolder.Folders["The Folder Name"] (where MAPIFolder is the parent folder) - you just need to know where it exists relative to the default folders. E.g. if it is on the same level as the Inbox, you can retrieve the Inbox folder using GetDefaultFolder(olFolderInbox), then use Inbox.Parent.Folders["The Folder Name"].

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

It seems that the Outlook MAPI Namespace (fetched with OutlookApplication.GetNamespace("MAPI")) has a collection of folders, which in their turn have a collection of folders, etc..

The folders are tree organized on the left pane of your Outlook application (for example if you have many emails accounts defined in your outlook each email addrees is an entry in your folder tree). So if you want to reach a specific folder you should get its relative number or get it by its name like said by Dimitri.

For example, you can reach email accounts with OutlookNamespace.Folders("xxxxx@yyyy.com"), then reach the desired subfolder either by number or by name. Hope this help.

Mooshua
  • 526
  • 2
  • 16