Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am using below code to move all unread emails to another folder from inbox in outlook.
But this code was not working properly, for example if 4 emails are in unread condition it moves only 2 emails to this folder .so it was failed for remaining 2 mails .
Can u please guide to resolve the issue.



C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Outlook;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
            Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI");
            Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
            //            Microsoft.Office.Interop.Outlook.MailItem newEmail = null;
            Microsoft.Office.Interop.Outlook.MailItem movemail = null;
            Microsoft.Office.Interop.Outlook.Items items = (Microsoft.Office.Interop.Outlook.Items)myInbox.Items;
            Microsoft.Office.Interop.Outlook.MAPIFolder destFolder = myInbox.Folders["Arch"];
            items.Restrict("[UnRead] = true");
            // Microsoft.Office.Interop.Outlook.MAPIFolder destFolder1 = mapiNameSpace.Folders["CQ Web"];
            foreach (object eMail in items.Restrict("[UnRead] = true"))
            {
                
                    movemail = eMail as Microsoft.Office.Interop.Outlook.MailItem;
                    if (movemail != null)
                    {

                        
                        movemail.Move(destFolder);
                        
                    }
                }                
            
        }
    }
}
Posted
Updated 24-Jul-18 1:04am
v2

That issue come because number of email in inbox folder will be degree each time you moved.To fix this error, don't use foreach function, please using for. Bellow is code I was used:
MAPIFolder inBox = this.Application.ActiveExplorer().Session.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            Items items = inBox.Items;
            //MailItem moveMail = null;
            //items.Restrict("[UnRead] = true");
            MAPIFolder destFolder = inBox.Folders["Fishing"];
            MessageBox.Show(items.Count.ToString());
            //MessageBox.Show(items.ToString());
            //while (items.Count != 0)
            //foreach (MailItem eMail in items)
            int ItemsCount = items.Count;
            for(int i=ItemsCount;i>0;i--)
            {
                try
                {
                    {
                        if (items[i].Subject.Contains("Fishing"))
                        {
                                items[i].Move(destFolder);
                            MessageBox.Show("Done!");
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("Error when moving email: "+ex);
                }
            }
 
Share this answer
 
For some reason the
C#
items.Restrict("[UnRead] = true")
won't work that way. I encountered this problem writing an outlook macro. The not-so-nice workaround to this is to use a while loop:
C#
while(items.Restrict("[UnRead] = true").Count > 0) {
    foreach(...)
 
Share this answer
 
v2
The above code which is correct it moves all the unread email to desired subfolder location , but at the same time it hard deleting unread email & email are getting dumped in "Restored deleted email from server "

Please provide me solution without getting dumped unread email to "restored delete email from server"

Thanks
 
Share this answer
 
Comments
CHill60 9-Jul-17 12:55pm    
If you have a question then use the "Ask a question" link!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900