Jake
Jake

Reputation: 3329

Grabbing Currently Selected Folder in Outlook Add-In

I'm working on an Outlook Add-In that can work in one of two ways, depending on a user's choice - it can process selected emails, or alternatively, process all emails in the selected folder. I've gotten the first part working, but the second part is giving me trouble, possibly because I'm just adapting the code from the first part incorrectly. I believe the trouble comes down to grabbing the currently selected folder properly in a C# Outlook add-in. I'm using .NET 3.5 and Outlook 2007, by the way.

First, the email code - if a user selects one or more emails in their inbox, and runs my add-in with the "selected emails" option, the following code is run (and works fine!):

public static void processSelectedEmails(Outlook.Explorer explorer)
{
    //Run through every selected email
    for (int i = 1; i <= explorer.Selection.Count; i++)
    //alternatively, foreach (Object selectedObject in explorer.Selection)
    {
        Object selectedObject = explorer.Selection[i];
    if (!(selectedObject is Outlook.Folder))
        {
                string errorMessage = "At least one of the items you have selected is not an email.";
                //Code for displaying the error
                return;
        }
        else
        Outlook.MailItem email = (selectedObject as Outlook.MailItem);
        //Do something with current email
    }
}

I've tried to adapt this code to do something else if a user goes to the Navigation Pane (on the left by default) in Outlook, selects a folder or subfolder (perhaps Inbox, Sent Items, or another folder they've created). The user can then choose the "process selected folder" option in my Add-In, which will do essentially the same thing as the code above, but process all of the email inside the selected folder. I have set it to only work if the user has selected a single folder.

public static void processFolder(Outlook.Explorer explorer)
{
    //Assuming they have selected only one item
    if (explorer.Selection.Count == 1)
    {
        //Make sure that that selected item is a folder
        Object selectedObject = explorer.Selection[1];
        if (!(selectedObject is Outlook.Folder))
        {
            string errorMessage = "The item you have selected is not a folder.";
            //Code for displaying the error
            return;
        }

        //Code for running through every email in that folder
    }
}

I have not yet written the code to actually run through all of the emails in the selected folder, because my code never gets past the if (!(selectedObject is Outlook.Folder)). Even if the most recently selected item is your Inbox, I receive the error I have programmed in at that point. Perhaps I am misusing the explorer.Selection thing? Any help would be much appreciated.

This may be important to answering my question - the add-in has a field called 'explorer', which is generated on startup: explorer = this.Application.ActiveExplorer. This is the 'explorer' that is passed to my functions so that they can know what is selected. As I said, this works fine for selected emails, but does not work for selected folders. Any insight would be greatly appreciated!

Edit 1: It appears that this question is basically a duplicate of Get all mails in outlook from a specific folder, but it has no answers.

Edit 2: I've been doing further research, it appears that I can get virtually the same functionality (but with an additional step unfortunately) by creating a popup to select a folder using the Application.Session.PickFolder() method. Is there any way to do it based on the currently selected folder, instead of forcing the user to pick a new folder?

Edit 3: I have modified the code found here: http://msdn.microsoft.com/en-us/library/ms268994(v=vs.80).aspx to further show what is not working properly for me:

  public static void processFolder(Outlook.Explorer explorer)
    {
        string message;
        if (explorer.Selection.Count > 0)
        {
            Object selObject = explorer.Selection[1];
            if (selObject is Outlook.MailItem)
            {
                message = "The item is an e-mail";
            }
            else if (selObject is Outlook.Folder)
            {
                message = "The item is a folder";
            }
            else
            {
                message = "No idea what the item is!";
            }

            Console.WriteLine(Message);
            return;
        }
    }

Whether I select a message, or go to the Navigation Pane and select a folder, I receive the message "This item is an e-mail".

Upvotes: 2

Views: 4550

Answers (1)

SliverNinja - MSFT
SliverNinja - MSFT

Reputation: 31641

Explorer.Selection is for Items only (MailItem, AppointmentItem, etc.) - not Folders. To get access to the currently selected Folder you would need Explorer.CurrentFolder.

Folder.Items would provide you access to all the Items in a given Folder.

Upvotes: 5

Related Questions