none
BeforeSave event does not cancel RRS feed

  • Question

  • Hello everyone,

    i got an issue with a VSTO implementation. We have a VSTO Word 2007 Document project with our extension. When the user presses the Save button some checks run and the Save process must be stopped. This works most of the times. Yet on MSO2010 and on specific MSO versions it does not.

    For testing purposes i created a blank VSTO Word 2007 Document project and added a BeforeSave event handler. So the class looks like that:

    public partial class ThisDocument
    {
    private void InternalStartup() {
    BeforeSave += ThisDocumentBeforeSave;
    }



    static void ThisDocumentBeforeSave(object sender, Microsoft.Office.Tools.Word.SaveEventArgs e) {
    e.Cancel = true;
    e.ShowSaveAsDialog = false;
    }
    }


    So nothing special...When i build this and open it in MSO2007 (in most cases, see below) the save button has no effect like expected. Yet when i open it with MSO2010 the save behaviour is back in the standard way. A save dialog shows up.

    I would somewhat understand if its limited to MSO2010 as its VSTO 3.0 and not 4.0 - yet this should at least work for all MSO2007 versions. But on some the same issue arise. Here is a list of version i tested:

    OK - Word 2007 12.0.4518.1014
    OK - Word 2007 12.0.6545.5000 SP2, MSO 12.0.6554.5001

    FAILURE - Word 2010 14.0.5128.5000 (x32)
    FAILURE - Word 2010 14.0.4760.1000 (x32)
    FAILURE - Word 2007 12.0.6545.5000 SP2, MSO 12.0.6535.5002

    On all machines VSTO 3.0 SP1 & PIA was installed.

    Is that a known bug within VSTO ? Using the same code within a new VSTO Word 2010 Document opened with MSO2010 works.


    Thanks for the support,
    Oliver



    Tuesday, July 26, 2011 10:53 AM

Answers

  • Hi Oliver,

    Thanks for your post.

    Have to try to set a breakpoint to detect whether the BeforeSave event is raised up by using issued version MSO?

    I think it is necessary to know the issue is caused by the Save action not raising up the evevnt or the Cancel parameter failing to cancel the event.

    In addition, have you tried to use Application.DocumentBeforeSave event, which can identify the event is raised up by Save or SaveAS via SaveAsUI parameter

    I look forward to hearing of you.

     


    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.

    • Marked as answer by oliver_zx Monday, August 1, 2011 4:16 PM
    Monday, August 1, 2011 9:40 AM
    Moderator

All replies

  • Hi Oliver

    You should not be putting code in the InternalStartup method. This code should be in the ThisDocument_Startup event. See what kind of difference moving it to the correct method makes...


    Cindy Meister, VSTO/Word MVP
    Tuesday, July 26, 2011 2:30 PM
    Moderator
  • Hi Cindy,

    thanks once more for the answer. Having the BeforeSave hook inside the startup event handler method does not change anything though. Same behaviour on both MSO2007 and MSO2010 as before.

    Kind regards,
    Oliver

    Wednesday, July 27, 2011 8:54 AM
  • Hello Oliver,

     

    It would be interesting to know if the event is hooking in Office2010. If you cannot debug, put a MessageBox.Show("Hooked") in there also, just to be sure it hooks on Office 2010 also.

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Wednesday, July 27, 2011 11:47 AM
  • Hi Silviu,

    yes it does hook. The initial test code i used included a popup - i just kept it out of the post to keep it as simple as possible.

    Kind regards,
    Oliver

    Wednesday, July 27, 2011 11:51 AM
  • One important addition i forgot to mention...it does work for the "Save" button, but not for the "Save As" button on the MSO versions mentioned as FAILURE above. for the others it works for both buttons. Yet in both cases my test popup in the BeforeSave event handler appears so the event itself is triggered correctly.

    Wednesday, July 27, 2011 11:59 AM
  • Hello Oliver,

     

    I found a post similar to what you desire http://social.msdn.microsoft.com/Forums/en/vsto/thread/7b936f49-191a-4ad9-bb6c-b5381fcfcb72 .

    The only difference was the fact that the line

    e.ShowSaveAsDialog = false; 

    comes before

    e.Cancel = true; 

    I do not know if this works but hey :) it is easy to try :p . The order is more logical like this, that first the window should be silenced, and then the event should be canceled.

     

    Best regards,

    Silviu.


    http://www.rosoftlab.net/
    Thursday, July 28, 2011 5:35 AM
  • Hi Silviu,

    thanks for the efforts. But both variants result in the same behaviour. As it does work for some Word versions and there is a difference between "Save" and "Save As" i suspect it might be really a bug in VSTO/Word  :/

    Kind regards,
    Oliver

    Thursday, July 28, 2011 6:51 AM
  • Hi Oliver,

    Thanks for your post.

    Have to try to set a breakpoint to detect whether the BeforeSave event is raised up by using issued version MSO?

    I think it is necessary to know the issue is caused by the Save action not raising up the evevnt or the Cancel parameter failing to cancel the event.

    In addition, have you tried to use Application.DocumentBeforeSave event, which can identify the event is raised up by Save or SaveAS via SaveAsUI parameter

    I look forward to hearing of you.

     


    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.

    • Marked as answer by oliver_zx Monday, August 1, 2011 4:16 PM
    Monday, August 1, 2011 9:40 AM
    Moderator
  • Hi Calvin,

    thanks for the reply. The event handler were reached correctly. I tested it with popups...But what does help is the use of Application.DocumentBeforeSave instead of Document.BeforeSave as you proposed.

    In both issue cases (MSO2010 and MSO2007) it does work now. So thanks alot for bring the attention to this other event. Yet i would have expected the same behaviour!

    Thanks again,
    Oliver

    Monday, August 1, 2011 4:16 PM
  • Hi,

    I had just the same issue in a VSTO Word 2007 document project running on Word 2010 and I have verified that it has been solved with Office 2010 SP1:

    FAILURE - Word 2010 14.0.4760.1000 (x32)

    OK - Word 2010 (14.0.6024.1000) SP1 MSO 14.0.6023.1000 (x32)

    Kind regards,

    Barbara

    Thursday, January 19, 2012 10:26 AM
  • Hi Barbara,

    good to know. Thanks for the info.

    Oliver

    Thursday, January 19, 2012 10:31 AM