none
How to save all open emails in draft folder and exit outlook 2007 without user input? RRS feed

  • Question

  • I'm writing a c# windows application form that needs to save all open emails(replies, forwards or new emails) and then close outlook 2007 for making a copy of pst file to a backup folder. Now it is saving all emails into draft folder but after I put the Quit command in my code it will close the outlook window only and the outlook.exe process is still in my processes list and i'm not able to copt the pst file. I've tried to kill the outlook.exe process but after this all my saved emails in draft folder disappeared! Any suggestions? Here is my code below:

        

    Outlook._Application app = new Outlook.Application(); foreach (Outlook.Inspector inspector in app.Inspectors) { Debug.Print(inspector.Caption); if (inspector.CurrentItem is Outlook.MailItem) { Outlook.MailItem mi = inspector.CurrentItem as Outlook.MailItem; mi.Save(); } } app.Quit(); File.Copy(filename, "c:\\backup\\outlook.pst", true);

    I'll really appreciate if anyone help me on this.

    Armin

    • Moved by Lisa Zhu Wednesday, January 16, 2013 2:29 AM outlook dev related
    Monday, January 14, 2013 8:30 PM

Answers

  • use code as in sample here http://msdn.microsoft.com/en-us/library/office/ff462097(v=office.14).aspx to get active outlook process. Now very important step - each COM instance that you obtain (here: MailItem , Inspector, Inspectors, Application) has to be released by calling Marshal.ReleaseComObject (since you are about to close outlook, you can even call FinalReleaseComObject to be sure). As for saving to draft - not all mails opened in inspectors are 'writeable' and therefore calling save on them will not save them to draft folder, but from your point of view it does not matter.

    Wednesday, January 16, 2013 7:21 AM

All replies

  • Hi Armin,

    This thread is Outlook specific question, I will move it to Outlook for Developers Forum for better support. You may have more lucky to get answer there.

    Thanks for your understanding.

    Regards,



    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 16, 2013 2:28 AM
  • Hi Armin,

    Thank you for posting in the MSDN Forum.

    I think you don't have to use

    Outlook._Application app = new Outlook.Application();

    if Outlook is already running. I would recommend you get the Outlook instance that is running.

    You can take a look at this page in which Cindy taught me a lesson.

    Although her posts are about get Word instance and basically in VB.Net, I think they will be very helpful to you as well.

    Hope it helps. If there is still any question, please feel free to let me know.

    Best regards,


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, January 16, 2013 5:18 AM
    Moderator
  • I think you need to copy first and then quit outlook
    File.Copy(filename, "c:\\backup\\outlook.pst", true);
    app.Quit();
    

    Wednesday, January 16, 2013 5:44 AM
  • use code as in sample here http://msdn.microsoft.com/en-us/library/office/ff462097(v=office.14).aspx to get active outlook process. Now very important step - each COM instance that you obtain (here: MailItem , Inspector, Inspectors, Application) has to be released by calling Marshal.ReleaseComObject (since you are about to close outlook, you can even call FinalReleaseComObject to be sure). As for saving to draft - not all mails opened in inspectors are 'writeable' and therefore calling save on them will not save them to draft folder, but from your point of view it does not matter.

    Wednesday, January 16, 2013 7:21 AM
  • In addition to what Damian mentioned, you need to avoid using a foreach() loop. That creates object variables that cannot be released. Use a for() loop instead with an index starting at Inspectors.Count and going down to 1. You also probably should close each open Inspector after you save the item in the Inspector, before you call the Quit() method.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "DamianD" <=?utf-8?B?RGFtaWFuRA==?=> wrote in message news:52beda54-d0aa-4c64-87a5-bba0abfb705b...

    use code as in sample here http://msdn.microsoft.com/en-us/library/office/ff462097(v=office.14).aspx to get active outlook process. Now very important step - each COM instance that you obtain (here: MailItem , Inspector, Inspectors, Application) has to be released by calling Marshal.ReleaseComObject (since you are about to close outlook, you can even call FinalReleaseComObject to be sure). As for saving to draft - not all mails opened in inspectors are 'writeable' and therefore calling save on them will not save them to draft folder, but from your point of view it does not matter.


    Ken Slovak MVP - Outlook
    Wednesday, January 16, 2013 3:21 PM
    Moderator
  • Thanks for the very helpful tips.

    i have one more question, the outlook is sometimes open and i'm creating new app object, do i have to do

     Outlook._Application app = new Outlook.Application();

    even when the outlook is open? if not what should i do?

    Thursday, January 17, 2013 8:04 PM
  • you should do exactly as in link i gave you, scroll down to see code example
    Thursday, January 17, 2013 10:11 PM