-2

I have written a function to iterate through the folders in outlook to retrieve the sub folder and download the attachment. the recursion function has been written in global code stage of initialize page and the main program i calling the function. What i want to understand is whether the code stage acts as a main() function? if so, then it should invoke the function which it is not.

I have written the recursion in global code. MultipleID m = new MultipleID(); MultipleID is the name of the object.

//initialize page code:
    public string folderPath =@"\\abc@outlook.com\test\test1\test2";
    public string subject ="test exist";
//path to save the attachment
     public path =@"C:\test\";
     public void ProcessFolder(MAPIFolder folder)
     {
          foreach(MAPIFolder subFolder in folder.Folders)
          {
              if(subFolder.FullFolderPath == folderPath)
              {
            }
     }
          ProcessFolder(subFolder);
     }

//code stage

    MultipleID m = new MultipleID();
    string EmailID = "abc@outlook.com";
    Microsoft.Office.Interop.Outlook.Application app = new 
    Microsoft.Office.Interop.Outlook.Application();
    Accounts acc = app.Session.Accounts;
    NamesSpace ns = app.GetNamespace("MAPI");
    Folder folder = app.Session.DefaultStore.GetRootFolder() as Folder;
     Microsoft.Office.Interop.Outlook.Stores stores = ns.Stores;
    foreach(Microsoft.Office.Interop.Outlook.Store store in stores)
    {

     if(store.DisplayName == EmailID)
     {
            MAPIFolder inbox_folder = 
            store.GetDefaultFolder(olDefaultFolders.OlFolderInbox);
            m.ProcessFolder(inbox_folder);
          }
     }    

1 Answers1

0

The recursive call of ProcessFolder doesn't have a valid input, since subFolder is out of scope. You need to either define a variable outside of your foreach and use that, or place ProcessFolder inside of your foreach. What I don't entirely understand here, is why you're looking for it if you already know the fixed path, unless it's to validate if it exists or not. If that's the case, you also need to modify the code stage.

Additionally, you would be better off referencing a folder by namespace types. On that note, it appears you have a typo by declaring ns as a NamesSpace instead of a NameSpace.

public bool ProcessFolder(MAPIFolder folder)
{
    bool found = false;
    foreach(MAPIFolder subFolder in folder.Folders)
    {
        if(subFolder.FullFolderPath == folderPath)
        {
            found = true;
            return found;
        }
        else
        {
            found = ProcessFolder(subFolder);
            if(found)
            {
                return found;
            }
        }
    }
    return found;
}



NameSpace ns = app.GetNamespace("MAPI");
Folder folder = ns.GetDefaultFolder(Outlook_Folder_ID)
foreach(Microsoft.Office.Interop.Outlook.Store store in stores)
{
    if(store.DisplayName == EmailID)
    {
        MAPIFolder inbox_folder = 
        store.GetDefaultFolder(olDefaultFolders.OlFolderInbox);
        if(ProcessFolder(inbox_folder))
        {
            foreach(string name in folderPath.Split("\"))
            {
                folder = folder.Folders(name);
            }
            //Proceed to do code here
        }
        else
        {
            //Throw an error, do something else.
        }
    }
}

Though ideally, you shouldn't actually need anything quite /that/ convoluted to begin with. If you already know the actual name of the subfolder, you can cut out the global code entirely. Suppose the subfolder name you're looking for is called "RPA". Let's also suppose you have a code stage input called "Sub_Folder" as Text.

NameSpace ns = app.GetNamespace("MAPI");
Folder folder = ns.GetDefaultFolder(Outlook_Folder_ID)
foreach(Microsoft.Office.Interop.Outlook.Store store in stores)
{
    if(store.DisplayName == EmailID)
    {
        MAPIFolder inbox_folder = 
        store.GetDefaultFolder(olDefaultFolders.OlFolderInbox);
        if(Sub_Folder != "")
        {
            //Since we supplied Sub_Folder, set it to the value. In this case, RPA.
            foreach(string name in Sub_Folder.Split("\"))
            {
                folder = folder.Folders(name);
            }
        }
    }
}

There's a few other optimizations that could (maybe should) be done, but these are what are at least relevant to your question.