0

I am new to C#. I am able to code in C# because I am a java programmer, and C# is also Object Oriented. Please bear with me as some of the names of the terminologies that I use here to explain might be of java.

How do I iterate among the subfolders using the folder method in C#? I have initialized the subFolders object of the method folders of the class Outlook to store all the subfolders if there are any. And I am using a for loop to iterate among them. In the loop, I am trying to initialize a subFolder object of the folder method to hold the current iterating folder from the subFolders object.

I am getting a conversion error while trying to assign the current iterating folder, to the subFolder object.

Error Message: Cannot implicitly convert type 'Microsoft.Office.Interop.Outlook.MAPIFolder' to 'Microsoft.Office.Interop.Outlook.Folder'. An Implicit conversion exists (are you missing a cast?)

Below is a part of the code:

 static void enumerateFolders(Outlook.Folder folder)  //Checks if there are sub folders inside the Inbox folder.
        {
            Outlook.Folders subfolders = folder.Folders;
            if (subfolders.Count > 0)
            {
                for (int i = 0; i < subfolders.Count; i++)
                {
                    Outlook.Folder subFolder = subfolders[i];  //This is where I am getting the error.
                    iterateMessages(subFolder);
                }
            }
            else
            {
                iterateMessages(folder); 
            }
        }
Gaurav Thantry
  • 753
  • 13
  • 30
  • Do you miss some tags? What is `Outlook.Folder`? Why not [Directory class](https://stackoverflow.com/q/5181405/1997232)? – Sinatr Feb 11 '19 at 12:50
  • Hi @Sinatr. Thank you for your prompt reply. I am trying to iterate a folder in an email account configure in outlook. – Gaurav Thantry Feb 11 '19 at 12:52
  • Shortest way to go : try to change the type of Outlook.Folder by var, if you like to know what type it would use, just put your mouse pointer over the variable name. var is a keyword for implicit type – Jimbot Feb 11 '19 at 12:55
  • Hi @Jimbot. Thank you for your help. It worked after I converted the type of `subfolders[i]` to `(Outlook.Folder) subfolders[i]`. But I still want to know your way of getting through this. How will I pass it on to the iterateMessages() method if I change the type of Outlook.Folder to var? – Gaurav Thantry Feb 11 '19 at 13:05
  • 1
    I suggest you use var just to know what type the compiler will infer. You can look at it when your mouse is over the variable name and then you use this type, which should also be the least specific type of your folder source. – Jimbot Feb 11 '19 at 14:05

2 Answers2

1

I used mapifolders, and this code has worked for many versions of outlook ( used this to build up the \\pst\folder\folder2 type paths we are used to)

public struct Flder
{
    public String name;
    public MAPIFolder folder;
}

...

private static void WalkTree(Folders topfolder, String path)
{
    if (topfolder.Count > 0)
    {
        foreach (MAPIFolder f in topfolder.AsParallel())
        {
            if (!f.Name.Contains("Public"))
            {
                Flder fld = new Flder();
                fld.name = path + "\\" + f.Name;
                fld.folder = f;
                folderList.Add(fld);
                try
                {
                    WalkTree(f.Folders, path + "\\" + f.Name);
                }
                catch
                {
                    continue; // skip any errors
                }
            }
        }
    }
}
BugFinder
  • 17,474
  • 4
  • 36
  • 51
0

I got the solution. All I had to do is typeCast subfolders[i] to Outlook.Folder type. Below is the code:

 static void enumerateFolders(Outlook.Folder folder)  //Checks if there are sub folders inside the Inbox folder.
        {
            Outlook.Folders subfolders = folder.Folders;
            if (subfolders.Count > 0)
            {
                for (int i = 0; i < subfolders.Count; i++)
                {
                    Outlook.Folder subFolder = (Outlook.Folder) subfolders[i];  //Solution: type casted
                    iterateMessages(subFolder);
                }
            }
            else
            {
                iterateMessages(folder);     //This implements the core functionality of the program. It iterates amongst the emails to retrieve the clearstream attachment.
            }
        }
Gaurav Thantry
  • 753
  • 13
  • 30