Question : Looping through Emails in an Outlook folder using For next loop in C#.net

I need to loop through an outlook folder using a For Next loop instead of a For Each loop. I can get the For Each to work but only half the emails actually move. I have two issues I need help with.
1. I want to do my For loop backwards (step -1 in VB) and I don’t know how to do that in C#
2. oMailItem1 = oFolderIn.Items(i); doesn’t work and I don’t know the proper syntax.

private void button1_Click(object sender, EventArgs e)
    {
      Outlook.Application oLk = new Outlook.Application();
      Outlook._NameSpace olNS = oLk.GetNamespace("MAPI");
      Outlook._Folders oFolders;
      oFolders = olNS.Folders;
      Outlook.MAPIFolder oFolder;
      oFolder = oFolders[1];
      Outlook.MAPIFolder oFolderIn = oFolder.Folders["NetixchangeIn"];
      Outlook.MAPIFolder oFolderOut = oFolder.Folders["NetixchangeOut"];
      Outlook.Items oItems = oFolderIn.Items;
      foreach (Outlook.MailItem oMailItem in oFolderIn.Items)
      {
        if (true)
        {
          oMailItem.Move(oFolderOut);
        }
      }
      //The above works but it gets messed up because it only moves half the emails
      int j = oFolderIn.Items.Count;  //There are 15 in test
      Outlook.MailItem oMailItem1;
      for ( int i = 1; i <= j; i++) //I want to go from 15 to 1 step = -1
      {
        oMailItem1 = oFolderIn.Items(i); //This produces an error
        oMailItem1.Move(oFolderOut);
      }
    }

Answer : Looping through Emails in an Outlook folder using For next loop in C#.net

Hi,

  Does this work for you....
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
            Outlook.Application oLk = new Outlook.Application();
            _NameSpace olNS = oLk.GetNamespace("MAPI");
            _Folders oFolders = olNS.Folders;
            MAPIFolder oFolder;
            oFolder = oFolders[1];
            MAPIFolder oFolderIn = oFolder.Folders["NetixchangeIn"];
            MAPIFolder oFolderOut = oFolder.Folders["NetixchangeOut"];
            MailItem oMailItem;
            for (int i = oFolderIn.Items.Count; i >= 0; i--)
            {
                oMailItem = (MailItem)oFolderIn.Items[i];
                oMailItem.Move(oFolderOut);
            }
Random Solutions  
 
programming4us programming4us