none
VSTO: Word: ThisDocument_BeforeSave() fired AFTER cached properties are saved RRS feed

  • Question

  • Hello,

    I want to report the following bug in the saving behavior of cached properties in Word 2007/2010 (VSTO)

    Consider the following example: (VS2008/VS2010, Office 2007/2010)

    public partial class ThisDocument
    {
        [Cached]
        public int nIntValue = 0;
    
        private void ThisDocument_Startup(object sender, System.EventArgs e)
        {
          MessageBox.Show(nIntValue.ToString());
        }
    
        private void ThisDocument_Shutdown(object sender, System.EventArgs e)
        {
        }
    
        //VSTO generated code omitted
    
        private void ThisDocument_BeforeSave(object sender, Microsoft.Office.Tools.Word.SaveEventArgs e)
        {
          nIntValue = 1;
        }
    }
    

     

    1st time you open the document the message box  displays 0 as expected.

    However, after you save the document and open it again, it still displays 0 instead of 1 that was intended.

    This indicates, that all the cached data fields, are saved before ThisDocument_BeforeSave() is executed and that all changes in those values that might take place inside the handler are ignored.

    Behind the hoods Word the event handler is triggered like this:

    [internal code that saves cached properties]
    ThisDocument_BeforeSave()
    [internal code that saves main document data except cached properties]

    while it should be triggered like this:

    ThisDocument_BeforeSave()
    [internal code that saves cached properties]
    [internal code that saves main document data except cached properties]

    This prohibits code inside ThisDocument_BeforeSave() to be able to alter the value of a cached property and save it.

    I have submitted this bug in Microsoft Connect site, but I was redirected here since the Office support team doesn't use MS Connect.

    http://connect.microsoft.com/VisualStudio/feedback/details/685616/vsto-word-thisdocument-beforesave-fired-after-cached-properties-are-saved

    Thank you

    Jason Orphanidis


    Jason Orphanidis
    • Moved by Max Meng Thursday, October 13, 2011 5:36 AM (From:Word IT Pro Discussions)
    Wednesday, October 12, 2011 7:02 AM

All replies

  • Hi Jason,

    Could you give us more details about the cached properties? I think after you have terminate the word application. The cached data will also be cleared. Then, you still get the same value.

    Hope this can give you the hint.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 17, 2011 6:48 AM
  • Hi Bruce,

    No that's not true. Cached properties are serialized on disk. Caching does not refer to memory (simple member variables do the work just fine!).

    So, the problem is with saving. They get saved before ThisDocument_BeforeSave()
     executes, instead of after.

    Jason


    Jason Orphanidis
    Tuesday, October 25, 2011 1:11 PM