none
C# to move each item in Sent Folder to A Folder in PST RRS feed

  • Question

  • I want to be able to use C# to access my outlook and move each message in my Sent Folder to a folder in my .PST called Sent Archive.  How can I achieve this?
    Monday, November 4, 2013 5:55 PM

Answers

  • Hello,

    I see that Outlook cannot locate the target folder. Outlook navigates the folder from root folder collection. For e.g. if Outlook is configured to have "Mail Folder ideally abc@xyz.com", "Archives", "Archives 2" etc... which in turn has "Inbox, Drafts and other folders", with session object, Outlook searches "Archive" folder in first level of "Mail Folder" and not the sub-folders. Navigate to the right folder collection, in this case loaded PST and navigate till the lower level. Like,

    Outlook.MAPIFolder olSource, olTarget;
    // Root folder (usually configured mail) - Sent Mail folder is selected
    olSource = olApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
    // Select Inbox folder from "Archives" collection, a loaded PST - further Inbox use .Folders["FolderName"]
    olTarget = olApp.Session.Folders["Archives"].Folders["Inbox"];
    Once the target folder is set, iterate to move the items from the source folder to target folder.

    Thanks, Pradeep Kumar R

    • Proposed as answer by mstechies Tuesday, November 5, 2013 4:31 PM
    • Marked as answer by Joe Marx's Tuesday, November 5, 2013 4:55 PM
    Tuesday, November 5, 2013 4:26 PM

All replies

  • Hello,

    Please check in the samples repository at http://1code.codeplex.com/. 


    Thanks, Pradeep Kumar R

    Monday, November 4, 2013 10:53 PM
  • Hello,

    Please check in the samples repository at http://1code.codeplex.com/. 


    Thanks, Pradeep Kumar R

    I found this code, but am getting a debug error when it reaches the oTarget line...now that could be bc this is a pst folder that I want to move to.  The debug error I get is posted below.

    using Outlook = Microsoft.Office.Interop.Outlook; public void MoveMyEmails() { //set up variables Outlook.Application oApp = null; Outlook.MAPIFolder oSource = null; Outlook.MAPIFolder oTarget = null; try { //instantiate variables oApp = new Outlook.Application(); oSource = oApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail); oTarget = oApp.Session.Folders["Archive"]; //loop through the folders items for (int i = oSource.Items.Count; i > 0; i--) { move the item oSource.Items[i].Move(oTarget); } } catch (Exception e) { //handle exception

    Console.WriteLine(e.ToString());

    } //release objects if (oTarget != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(oTarget); GC.WaitForPendingFinalizers(); GC.Collect(); } if (oSource != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(oSource); GC.WaitForPendingFinalizers(); GC.Collect(); } if (oApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(oApp); GC.WaitForPendingFinalizers(); GC.Collect(); } }

    System.Runtime.InteropServices.COMException (0x8004010F): The operation failed. An object could not be found.

    at Microsoft.Office.Interop.Outlook.FoldersClass.get_Item(Object Index)

    at ConsoleApplication1.Program.MoveSentMessages()


    • Edited by Joe Marx's Tuesday, November 5, 2013 1:37 PM
    Tuesday, November 5, 2013 1:35 PM
  • Hello,

    I see that Outlook cannot locate the target folder. Outlook navigates the folder from root folder collection. For e.g. if Outlook is configured to have "Mail Folder ideally abc@xyz.com", "Archives", "Archives 2" etc... which in turn has "Inbox, Drafts and other folders", with session object, Outlook searches "Archive" folder in first level of "Mail Folder" and not the sub-folders. Navigate to the right folder collection, in this case loaded PST and navigate till the lower level. Like,

    Outlook.MAPIFolder olSource, olTarget;
    // Root folder (usually configured mail) - Sent Mail folder is selected
    olSource = olApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);
    // Select Inbox folder from "Archives" collection, a loaded PST - further Inbox use .Folders["FolderName"]
    olTarget = olApp.Session.Folders["Archives"].Folders["Inbox"];
    Once the target folder is set, iterate to move the items from the source folder to target folder.

    Thanks, Pradeep Kumar R

    • Proposed as answer by mstechies Tuesday, November 5, 2013 4:31 PM
    • Marked as answer by Joe Marx's Tuesday, November 5, 2013 4:55 PM
    Tuesday, November 5, 2013 4:26 PM