none
Discard changes on Outlook.MailItem RRS feed

  • Question

  • Hi,

    in my addin, i'm writing additional information into the mileage property. This works fine for all items with write permissions. But in public folders with only read permission, saving the mail fails with ErrorCode 2147024891 - access denied. In this case, i want to discard my changes to prevent the "item was changed" save dialog. Therefore i close and release the object within an async function:

    private delegate void discardMailItemAsyncDelegate(MailItem item);
    
    private void dicardMailItemAsync(MailItem item)
    {
       mail.Close(OlInspectorClose.olDiscard);
       Marshal.FinalReleaseComObject(mail);
       mail = null;
       GC.Collect();
    }
    
    public override void close()
    {
       if (mail != null)
       {
         var insp = mail.GetInspector;
         insp.Close(OlInspectorClose.olDiscard);
         Marshal.FinalReleaseComObject(insp);
         insp = null;
    
         discardMailItemAsyncDelegate dicard = new discardMailItemAsyncDelegate(this.dicardMailItemAsync);
         dicard.BeginInvoke(mail, null, null);
       }
       GC.Collect();
    }
    But when closing Outlook, the save dialog is still present :( Anyone a solution for me?

    All the best, Josef


    Mache die Dinge so einfach wie möglich, aber nicht einfacher!

    Thursday, September 15, 2016 11:22 AM

Answers

  • Hi,
    the problem is caused by a toolbar-button, added by my addin. if the last mail exported is located in a shared, readonly folder the message prompts. otherwise outlook closes fine. more about this at ansers.microsoft.com

    Mache die Dinge so einfach wie möglich, aber nicht einfacher!

    Thursday, September 22, 2016 7:10 AM

All replies

  • Hello Joau,

    You should deal with the Outlook object model on the main thread only. Office applications uses the single threaded apartment model which doesn't allow multithreading. You can process any .Net objects and other primitives (not OOM) on secondary threads.

    Also I'd recommend reviewing the existing code base and releasing underlying COM objects instantly. Use System.Runtime.InteropServices.Marshal.ReleaseComObject to release an Outlook object when you have finished using it. Then set a variable to Nothing in Visual Basic (null in C#) to release the reference to the object. Read more about that in the Systematically Releasing Objects article. There is no need to use GC methods in that case.

    Anyway, if you want to use GC methods for swiping the heap you need to call the following methods twice:

    GC.Collect();
    GC.WaitForPendingFinalizers();

    There are some drawbacks. First, it is an implicit way. That is, you can’t control the result. Secondly, you have to run the GC spending some time on this. More than that, the way in which .NET objects are stored in memory requires you to run the GC twice. You might want to read When to call GC.Collect() at Rico Mariani’s Performance Tidbits where you find why it isn’t recommended to call GC.Collect().


    [custom.development]

    Thursday, September 15, 2016 1:19 PM
  • Hi Eugene,

    thank you for your quick response! I've adapted my code, according to your advice, but i still get the save-dialog at the end of my outlook session.  Here is my catchblock in the save method with : 

    catch (System.UnauthorizedAccessException ex) {

    mail.Mileage = "Foo"; mail.Close(OlInspectorClose.olDiscard); Debug.Print(mail.Mileage); //<-- mail.Mileage is null Marshal.ReleaseComObject(mail); mail = null;

    }

    if i load the item again after the failed save process, i can't find a difference to the original item. would it be possible to check the write access on shared folders, before making changes to the item?

    Thank you, Josef


    Mache die Dinge so einfach wie möglich, aber nicht einfacher!

    Friday, September 16, 2016 8:52 AM
  • Hi,
    the problem is caused by a toolbar-button, added by my addin. if the last mail exported is located in a shared, readonly folder the message prompts. otherwise outlook closes fine. more about this at ansers.microsoft.com

    Mache die Dinge so einfach wie möglich, aber nicht einfacher!

    Thursday, September 22, 2016 7:10 AM
  • Hi,

    Thanks for sharing the solution with us.

    If you have any other issues, please feel free to post threads, many community members would be willing to help you.

    Regards,

    Celeste

    Thursday, September 22, 2016 11:57 AM
    Moderator