none
How to call some code after Document is saved in Word? RRS feed

  • Question

  • Hi,

    I need to do some processing in a VSTO Word addin after a document is saved. From the online documentation it seems that there is only a DocumentBeforeSave in Word. There is no corresponding DocumentAfterSave event.

    My plan was to add a handler for the DocumentBeforeSave event in the startup event of the addin. Then, when the DocumentBeforeSave is called, somehow post an event/function from there that will be called after the DocumentBeforeSave event is completed. Is this possible? Is there any sample code in C# to illustrate this? I need to do some processing on the document after it is saved.

     

    My addin is written in C# using VS2008 and is targeted at Word2007.

    Thanks

    Rajeev

    Friday, June 17, 2011 12:52 PM

Answers

  • I found a code sample at this url:

    http://davecra.wordpress.com/2011/05/05/word-aftersave-event/

    It seems to satisfy my needs for the present, although I need to do more comprehensive testing.

    • Marked as answer by Bruce Song Monday, June 27, 2011 4:03 AM
    Thursday, June 23, 2011 4:02 PM

All replies

  • Hello,

     

    What you can do is call the document.Save() method in the before save event.

    If the document.Saved==true after the method is called, then do what you need to do.

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Friday, June 17, 2011 1:17 PM
  • Hi Silviu,

    If I call document.Save in DocumentBeforeSave won't it call the event again and again and thus end up in an infinite loop?

    Thanks

    Friday, June 17, 2011 1:55 PM
  • Hi Silviu,

    If I call document.Save in DocumentBeforeSave won't it call the event again and again and thus end up in an infinite loop?

    Thanks

    Hi rajeevcrk,

     

    Thanks for your post.

    The Save() method in DocumentBeforeSave event doesn’t fire the event which the method embedded in. Actually, we usually change the cancel parameter to decide whether to save the document instead of calling the Save method again.

     

    Meanwhile, I would like to ask you what do you want to accomplish after the document has been saved.

     

    I look forward to hearing of you soon.


    Best Regards, Calvin Gao [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, June 20, 2011 6:24 AM
    Moderator
  • Hi Calvin,

     

    After the document is changed and saved I need to make a "diff" of the document into a separate folder (which will contain previous versions of the document). This needs to occur every time the user saves the document. So I need to read the document only after it has been saved - not before.

     

    Thanks

    Rajeev

    Wednesday, June 22, 2011 1:37 PM
  • Hi Rajeev

    Make the "diff" before the document is saved - the content isn't going to change in the meantime. If the user cancels the save, delete the "diff".


    Cindy Meister, VSTO/Word MVP
    Wednesday, June 22, 2011 1:40 PM
    Moderator
  • I found a code sample at this url:

    http://davecra.wordpress.com/2011/05/05/word-aftersave-event/

    It seems to satisfy my needs for the present, although I need to do more comprehensive testing.

    • Marked as answer by Bruce Song Monday, June 27, 2011 4:03 AM
    Thursday, June 23, 2011 4:02 PM