none
Programmatically loop through all existing SENT ITEMS and copy to new folder RRS feed

  • Question

  • I want to open with the fact that I am not a programmer.

    I'm looking to have a VSTO deployed through group policy to all users at my company. I need the VSTO to do a couple of steps:
    1. Create a new folder called, "Old Sent Mail", if already exist ignore
    2. Programmatically loop through all existing SENT ITEMS 
    3. Copy Sent Items to a "Old Sent Mail". If sent mail has already been copied to new folder, it should ignore it. 
    The program needs to be trigger each time Outlook has been restarted.


    If someone is doing something similar to this, can you provide a copy of your code that I can work from? Thanks ahead of time for any help with this.

    Outlook rules will not meet my needs because they are client-based rather than server-based. This is for retention purposes, because Sent Items are deleted after 180 days by Exchange administrators.

    Monday, November 28, 2016 4:16 PM

Answers

  • Hi dgastonumich,

    as example code is already suggested by the Eugene  Astafiev can help you to develop the code.

     below is an another examples of code that can help you to get an idea.

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.NewMail += new Microsoft.Office.Interop.Outlook.
                    ApplicationEvents_11_NewMailEventHandler
                    (ThisAddIn_NewMail);
    
            }
    
            private void ThisAddIn_NewMail()
            {
                Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)this.Application.
                    ActiveExplorer().Session.GetDefaultFolder
                    (Outlook.OlDefaultFolders.olFolderInbox);
                Outlook.Items items = (Outlook.Items)inBox.Items;
                Outlook.MailItem moveMail = null;
                items.Restrict("[UnRead] = true");
                Outlook.MAPIFolder destFolder = inBox.Folders["Test"];
                foreach (object eMail in items)
                {
                    try
                    {
                        moveMail = eMail as Outlook.MailItem;
                        if (moveMail != null)
                        {
                            string titleSubject = (string)moveMail.Subject;
                            if (titleSubject.IndexOf("Test") > 0)
                            {
                                moveMail.Move(destFolder);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
    

    Reference:

    How to: Programmatically Move Items in Outlook

    below is the VBA code , you need to convert it to c#.

    Sub CopyItem() 
     
     Dim myNameSpace As Outlook.NameSpace 
     
     Dim myFolder As Outlook.Folder 
     
     Dim myNewFolder As Outlook.Folder 
     
     Dim myItem As Outlook.MailItem 
     
     Dim myCopiedItem As Outlook.MailItem 
     
     
     
     Set myNameSpace = Application.GetNamespace("MAPI") 
     
     Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox) 
     
     Set myNewFolder = myFolder.Folders.Add("Saved Mail", olFolderDrafts) 
     
     Set myItem = Application.CreateItem(olMailItem) 
     
     myItem.Subject = "Speeches" 
     
     Set myCopiedItem = myItem.Copy 
     
     myCopiedItem.Move myNewFolder 
     
    End Sub
    

    MailItem.Copy Method (Outlook)

    try to understand the all the demo examples it can help you to fulfill your requirement.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by dgastonumich Tuesday, November 29, 2016 6:27 PM
    Tuesday, November 29, 2016 2:05 AM
    Moderator

All replies

  • Hello,

    To get a ready-made code you need to hire a developer. Forum is not the right place for requesting the code. Others and I can explain the required actions to get the job done. 

    > 1. Create a new folder called, "Old Sent Mail", if already exist ignore

    To check whether a folder exists you can try accessing it using the Folders indexer property. To create a subfolder you can use the following code:

    Outlook.NameSpace nameSpace = OutlookApp.GetNamespace("MAPI");
          Outlook.MAPIFolder folderInbox = nameSpace.GetDefaultFolder(
                Outlook.OlDefaultFolders.olFolderInbox);
          Outlook.Folders inboxFolders = folderInbox.Folders;
          Outlook.MAPIFolder subfolderInbox = null;
          try
          {
                subfolderInbox = inboxFolders.Add("InboxSubfolder", 
                     Outlook.OlDefaultFolders.olFolderInbox);
          }
          catch (COMException exception)
          {
                if (exception.ErrorCode == -2147352567)
                     //  Cannot create the folder.
                     System.Windows.Forms.MessageBox.Show(exception.Message);
           }
           if (subfolderInbox != null) Marshal.ReleaseComObject(subfolderInbox);
           if (inboxFolders != null) Marshal.ReleaseComObject(inboxFolders);
           if (folderInbox != null) Marshal.ReleaseComObject(folderInbox);
           if (nameSpace != null) Marshal.ReleaseComObject(nameSpace);

    See How To: Create a new folder in Outlook for more information. 

    2. Programmatically loop through all existing SENT ITEMS

    foreach (object item in items)
    {
        if (item is Outlook.MailItem)
        {
            ///The rest of your code
        }
    }

    Take a look at the similar forum threads for more information:

    How to loop through all MailItems of certain Outlook subfolders

    Outlook Interop: How to iterate over all items in folder

    > 3. Copy Sent Items to a "Old Sent Mail". If sent mail has already been copied to new folder, it should ignore it. The Move method of the MailItem class moves a Microsoft Outlook item to a new folder. 


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Monday, November 28, 2016 7:32 PM
  • Thank you!
    Monday, November 28, 2016 7:40 PM
  • Hi dgastonumich,

    as example code is already suggested by the Eugene  Astafiev can help you to develop the code.

     below is an another examples of code that can help you to get an idea.

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
            {
                this.Application.NewMail += new Microsoft.Office.Interop.Outlook.
                    ApplicationEvents_11_NewMailEventHandler
                    (ThisAddIn_NewMail);
    
            }
    
            private void ThisAddIn_NewMail()
            {
                Outlook.MAPIFolder inBox = (Outlook.MAPIFolder)this.Application.
                    ActiveExplorer().Session.GetDefaultFolder
                    (Outlook.OlDefaultFolders.olFolderInbox);
                Outlook.Items items = (Outlook.Items)inBox.Items;
                Outlook.MailItem moveMail = null;
                items.Restrict("[UnRead] = true");
                Outlook.MAPIFolder destFolder = inBox.Folders["Test"];
                foreach (object eMail in items)
                {
                    try
                    {
                        moveMail = eMail as Outlook.MailItem;
                        if (moveMail != null)
                        {
                            string titleSubject = (string)moveMail.Subject;
                            if (titleSubject.IndexOf("Test") > 0)
                            {
                                moveMail.Move(destFolder);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
    

    Reference:

    How to: Programmatically Move Items in Outlook

    below is the VBA code , you need to convert it to c#.

    Sub CopyItem() 
     
     Dim myNameSpace As Outlook.NameSpace 
     
     Dim myFolder As Outlook.Folder 
     
     Dim myNewFolder As Outlook.Folder 
     
     Dim myItem As Outlook.MailItem 
     
     Dim myCopiedItem As Outlook.MailItem 
     
     
     
     Set myNameSpace = Application.GetNamespace("MAPI") 
     
     Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox) 
     
     Set myNewFolder = myFolder.Folders.Add("Saved Mail", olFolderDrafts) 
     
     Set myItem = Application.CreateItem(olMailItem) 
     
     myItem.Subject = "Speeches" 
     
     Set myCopiedItem = myItem.Copy 
     
     myCopiedItem.Move myNewFolder 
     
    End Sub
    

    MailItem.Copy Method (Outlook)

    try to understand the all the demo examples it can help you to fulfill your requirement.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by dgastonumich Tuesday, November 29, 2016 6:27 PM
    Tuesday, November 29, 2016 2:05 AM
    Moderator