Gaurav Thantry
Gaurav Thantry

Reputation: 803

Cannot implicitly convert type 'Microsoft.Office.Interop.Outlook.MAPIFolder' to 'Microsoft.Office.Interop.Outlook.Folder'

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); 
            }
        }

Upvotes: 0

Views: 1148

Answers (2)

Gaurav Thantry
Gaurav Thantry

Reputation: 803

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

Upvotes: 0

BugFinder
BugFinder

Reputation: 17868

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

Upvotes: 1

Related Questions