none
Problem on moving items to specific folder RRS feed

  • Question

  • Hello,

    I'm working on outlook add in, using vs 2010, and c#.

    I want to move a selected item(s) to specific folder, by clicking a button, in which I put the folder path destination in the button tag(to allow to users to change it).

    the problem is when I click this button, the item is moved, with the 2 next items, but I want to move only emails that I selected. And at the end, i've an error "The operation failed"

    This is the code:

    <button id="AML" label="AML" onAction="OnClick" tag="folder path destination" />

      public void EnumerateFolders(Outlook.Folder folder)
            {
                Outlook.Folders childFolders = folder.Folders;
                if (childFolders.Count > 0)
                {
                    foreach (Outlook.Folder childFolder in childFolders)
                    {
                        arrText1.Add(childFolder);
                        EnumerateFolders(childFolder);
                    }
                }
            }       

        public void OnClick(Office.IRibbonControl control)
            {
                string username = Environment.UserName;
                Outlook.MailItem email = null;
                string currentUser = Globals.ThisAddIn.Application.Session.CurrentUser.AddressEntry.GetExchangeUser().Name;
                Outlook.MAPIFolder publicFolders = (Outlook.MAPIFolder)Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders);
                string ButtonPath = control.Tag;
                Outlook.Folder root = publicFolders as Outlook.Folder;
                EnumerateFolders(root);
            
                foreach (Folder destination in arrText1)
                {
                    if (ButtonPath == destination.FolderPath)
                    {
                        foreach (var mail in Globals.ThisAddIn.Application.ActiveExplorer().Selection)
                        {
                            try
                            {
                                email = mail as Outlook.MailItem;
                                if (email != null)
                                {
                                    //Dispatch.Value = currentUser;
                                    //DueDate.Value = DateTime.Now;
                                    email.Move(destination);
                                }
                            }
                            catch (SystemException ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                    }
                }               
            }

    Does anyone know where is the problem?

    Thanks

    Monday, September 16, 2013 3:49 PM

Answers

  • When you move or delete items in a loop never use a for loop or a foreach loop. Use a down counting for loop:

    for (i = ActiveExplorer().Selection.Count; i >= 1; i--)

    {

              // now do the assignment to the item referenced by the index value and move it

    }

    Move is a function, so you should use it that way and set an object variable to the moved item.


    Ken Slovak MVP - Outlook

    Monday, September 16, 2013 6:52 PM

All replies

  • When you move or delete items in a loop never use a for loop or a foreach loop. Use a down counting for loop:

    for (i = ActiveExplorer().Selection.Count; i >= 1; i--)

    {

              // now do the assignment to the item referenced by the index value and move it

    }

    Move is a function, so you should use it that way and set an object variable to the moved item.


    Ken Slovak MVP - Outlook

    Monday, September 16, 2013 6:52 PM
  • Hi Ken, thamks for the answer. I tried to follow what you said but I get this error

    "Retrieving the COM Class factory for the component with CLSID..."

    this is the code

    //to get all the folders in public folders and compare it to the destination path

    foreach (Folder destination in arrText1)
                {
                    if (ButtonPath == destination.FolderPath)
                    {
                        for (int i = Globals.ThisAddIn.Application.ActiveExplorer().Selection.Count; i >= 1; i--)
                        {
                            try
                            {
                                Outlook.Selection selection = Globals.ThisAddIn.Application.ActiveExplorer().Selection;

                                Outlook.MailItem mail = new Outlook.MailItem(selection[i]);
                                email = mail as Outlook.MailItem;
                                if (email != null)
                                {
                                    email.Move(destination);
                                }
                            }
                            catch (SystemException ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                    }
                }   

    Tuesday, September 17, 2013 6:25 AM
  • I replaced the code bellow with this code and it seems to work when I move mails in inbox to public folders folders. But I have the same problem when i try to move items from folder in public folders to its subfolders.

      foreach (Folder destination in arrText1)
                {
                    if (ButtonPath == destination.FolderPath)
                    {
                        //foreach (var mail in Globals.ThisAddIn.Application.ActiveExplorer().Selection)
                        for (int i = Globals.ThisAddIn.Application.ActiveExplorer().Selection.Count; i >= 1; i--)
                        {
                            try
                            {

                                object item = Globals.ThisAddIn.Application.ActiveExplorer().Selection[1];
                                MessageBox.Show(item.ToString());
                                if (item is MailItem)
                                {
                                    email = item as MailItem;
                                    //Dispatch.Value = currentUser;
                                    //DueDate.Value = DateTime.Now;
                                    email.Move(destination);
                                }

                            }
                            catch (SystemException ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                    }
                }

    Tuesday, September 17, 2013 11:44 AM
  • If you step the code in the loops, does everything point to the correct locations and items?

    What exact failure are you seeing? Are you getting an exception or what?

    Do you have permissions to move the items to where you want?

    What I meant about using Move() as a function is something like this:

    // outside the loops

    Outlook.MailItem dummy = null;

    // in the loop:

    dummy = email.Move(destination);

    dummy = null;


    Ken Slovak MVP - Outlook

    Tuesday, September 17, 2013 2:31 PM
  • I tried that. With this code, I could move item(s) to the desired folder, when it comes from inbox to public folders. The issue is when  I try to move item(s) from public folder to public subfolders.

    It moves the 3 first items(I want to move only selected items) and an exception occurs.

    "The operation failed".

    foreach (Folder destination in arrText1)
                {
                    if (ButtonPath == destination.FolderPath)
                    {
                        object item = Globals.ThisAddIn.Application.ActiveExplorer().Selection[1];
                        for (int i = Globals.ThisAddIn.Application.ActiveExplorer().Selection.Count; i >= 1; i--)
                        {
                            try
                            {
                                if (item is MailItem)
                                {
                                    email = item as MailItem;
                                    email.Move(destination);
                                }

                            }
                            catch (SystemException ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                    }
                }

    Tuesday, September 17, 2013 3:38 PM
  • I don't know why your code is failing after 3 iterations. That's a matter for debugging.

    I would step the code in the loops and verify that arrText1 contains all the entries you expect and that each is valid.

    I'd also check that each folder and folder path is valid and that for public folder subfolders that each path and folder is valid.

    I'd make sure each step along the way works, and that each iteration returns a mail item as expected.

    There are cases where things don't look wrong or non-working and all you can do is to debug and see what's going on. This looks like one of those cases.


    Ken Slovak MVP - Outlook

    Tuesday, September 17, 2013 6:04 PM
  • Thanks a lot, I will check differently if a path exists in folder 
    Thursday, September 19, 2013 9:22 AM