2

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

Community
  • 1
  • 1
Jake
  • 3,142
  • 4
  • 30
  • 48

1 Answers1

5

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.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
  • Interesting. In modifying my code, I found that there is a slight issue with using two different methods in this manner - similar to the code in Edit 3 which I have posted, how would I go about seeing a user's most recently selected item? Essentially what I'm saying is that if I select an email, that email will be in a folder, and `Explorer.CurrentFolder` will return to me the folder it's in. If I only select a folder, `Explorer.CurrentFolder` will also return that folder. How would I go about determining whether the last thing a user selected was a folder, or an email in that folder? – Jake Jul 13 '12 at 13:57
  • You probably need (2) different buttons since selecting a `Folder` automatically selects the first `Item` in that `Folder`. You could also consider using context menus on Items and Folders. – SliverNinja - MSFT Jul 13 '12 at 14:03
  • 1
    I'm actually currently using an add-in (http://www.techhit.com/messagesave/) which correctly identifies whether the last selected item was a folder or email, without the additional button. It just says "Save Selected Messages" or "All messages in folder 'FolderName'" at the top, depending on what the most recently selected item is. It's that functionality that I'm trying to duplicate without the user having to tell me explicitly – Jake Jul 13 '12 at 14:08
  • You would probably need to hook into the application events [`Explorer.FolderSwitch`](http://msdn.microsoft.com/en-us/library/ff865625) and [`Explorer.SelectionChange`](http://msdn.microsoft.com/en-us/library/ff869813) to interpret the last user action / current selection state. – SliverNinja - MSFT Jul 13 '12 at 14:22
  • But FolderSwitch event calls SelectionChange event. and i want to use both events, like user can select Folder or EmailItem. – Ashu Dec 09 '14 at 11:12