Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4047066
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:38:39+00:00 2026-05-20T13:38:39+00:00

My add-in is using Visual Studio 2010 targeting Outlook 2007. One task of the

  • 0

My add-in is using Visual Studio 2010 targeting Outlook 2007. One task of the add-in is to move all the mailitems from a folder to a designated folder.

My first phase of testing simply modified the subject and saved the email in the same folder. No problems there.

When I changed from the .Save method to use the .Move method, I cannot process all of the items. Some get moved then the foreach loop exits prematurely without throwing any exception.

My test data appears to me to quite controlled in that I move items manually into the folder I want to process before I step thru the code with the debugger. For example, with 4 mailitems in the folder, I can move 2 of them and when the foreach is revisited it goes to exit the loop leaving 2 mailitems unprocessed.

Any ideas?

        if (ProcessFolder(theRootFolder.FolderPath) && (theRootFolder.Items.Count > 0)) 
        {
            int itemCount = theRootFolder.Items.Count;
            int loopCount = 0;
            MetaDataForm getMetaData = new MetaDataForm(theRootFolder.FolderPath, theRootFolder.Items.Count);
            getMetaData.ShowDialog();
            if (getMetaData.DialogResult == DialogResult.OK)
            {
                string d1 = getMetaData.Meta1;
                string d2 = getMetaData.Meta2;
                try
                {
                    foreach (Object item in theRootFolder.Items)
                    {
                        loopCount++;
                        Outlook.MailItem mi = item as Outlook.MailItem;
                        if (mi != null)
                        {
                            mi.Move(_TRIM_archiveFolder);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ex.ToString();      
                    MessageBox.Show(String.Format("ItemCount={0},LoopCount={1},ExceptionData={2}", 
                                                                itemCount, loopCount, ex));
                }
                MessageBox.Show(String.Format("ItemCount={0},LoopCount={1}",
                                                                itemCount, loopCount));
             }
         }

EDIT:

Great idea….looping backwards solved the problem:

              // iterating backwards is needs because of .move below
                    for (int i = theRootFolder.Items.Count; i > 0; i--)
                    {
                        Outlook.MailItem mi = (Outlook.MailItem)theRootFolder.Items[i];
                        if (mi != null)
                        {
                            if (!mi.Subject.StartsWith("M1"))
                            {
                                mi.Move(_TRIM_archiveFolder);
                            }
                        }
                    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:38:40+00:00Added an answer on May 20, 2026 at 1:38 pm

    Generally when using foreach, the runtime does not like it too much when the collection being iterated changes.

    I’m taking a wild guess here, but I think the reason why you can move 2 out of your 4 items is that when you come to your third iteration the two items that are left has moved in the Items collection and are now on position 0 and 1, while you are looking at position 2.

    Use a for loop or a while loop with Items.Count() > 0 or something like that.

    I’m also guessing that you in fact are getting an exception when this happens, but it never pops up to you. Try to attach the process in WinDbg and see what’s going on. You are probably getting a COM Error of some kind.

    Aren’t the Outlook APIs pretty much still all COM libraries?

    EDIT:

    Your idea was right. Gave me the idea to iterate backwards:

                  // iterating backwards is needed because of .move below
                        for (int i = theRootFolder.Items.Count; i > 0; i--)
                        {
                            Outlook.MailItem mi = (Outlook.MailItem)theRootFolder.Items[i];
                            if (mi != null)
                            {
                                if (!mi.Subject.StartsWith("M1"))
                                {
                                    mi.Move(_TRIM_archiveFolder);
                                }
                            }
                        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just built an MS Outlook Add In using Visual Studio and Office 2010.
I am using Visual Studio 2010 to create a Outlook 2010 Add In. I
Context: I am building an Add-in using visual studio 2010. One of the actions
I have developed a word 2007 add-in using Visual Studio 2010, everything is working
We are building Office 2007 add-ins using Visual Studio 2008. Our builds are performed
Using Visual Studio 2008 - I add a Setup project (from Setup and Deployment
I'm using Visual Studio 2010 with some add-ins and extensions. The problem is now
All: We are using Visual Studio 2010, and we have recently upgraded our workstations
Using Visual Studio 2010 I created an Office 2010 Word add-in project that has
I'm using Visual Studio 2010 Ultimate and I know there's the Qt VS Add-In.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.