Search code examples
outlookvstooutlook-addinoffice-interopoffice-addins

VSTO Outlook: Iterate over Outlook.OlDefaultFolders enumeration and store entry IDs in a list


I would like to iterate over Outlook.OlDefaultFolders enumeration and get the ID entry for each default Outlook folder and store them in a list of Entry IDs. How can I achieve this?


Solution

  • That sounds like a generic .net programming question and the answer depends on the .net target version you use for the product. There are several ways to iterate over the enumeration values, you can find all of them describes in the How to enumerate an enum thread. For exaple:

    foreach (OlDefaultFolders defaultFolder in (OlDefaultFolders[]) Enum.GetValues(typeof(OlDefaultFolders)))
    {
        folder = namespace.GetDefaultFolder(defaultFolder);
    }
    

    Enumerations provide a convenient way to work with sets of related constants, and to associate constant values with names. To iterate through an enumeration, you can move it into an array using the GetValues method. You could also iterate through an enumeration using a foreach statement, using the GetNames or GetValues method to extract the string or numeric value.