none
BuiltIn Word Dialog SaveChanges RRS feed

  • Question

  • Hello,

    I need to check if the Internal Word Dialog "Do you want to save changes?" was displayed during my event triggered function DocumentBeforeClose.

    First i added my BeforeClose Event. After that i have no clue howto figure out if the Dialog was displayed or how to get the results. Is it possible to do that? I just have the information if the Doc was saved, or if the user canceled a Dialog.
     this.Application.DocumentBeforeClose += new Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(DocumentBeforeClose);

    void DocumentBeforeClose(Word.Document Doc, ref bool Cancel)
    {       
    if (Cancel == true);       
    // howto check if the user clicked Save, dont Save?
    }

    Greetings  SRH
    Wednesday, July 15, 2015 7:07 PM

Answers

  • Hi,

    To my first question:
    Like the People said. You have to set Cancel = True and the Word Dialog wont be displayed.
    But other plugins might present a dialog before you set Cancel = True. Like in my case.

    Second question:
    My "Workaround" is no real trick to track other plugins. I just ignored the other Dialog and displayed my own where I can handle the input.

    Best Regards.
    • Marked as answer by L.HlModerator Thursday, August 6, 2015 2:24 AM
    Wednesday, August 5, 2015 10:31 PM

All replies

  • Hi SRH

    If you need to interact with the user on this, then YOU set cancel=true in your code. That will prevent Word from closing the document and the message won't be shown. Instead, YOU show a message, asking whether the user wants to save changes, not save changes or cancel closing the document. Then your code handles saving (or not) and closing the document, based on what the user chooses. For the Close method, be sure to set the save option appropriately:

    Doc.Close Word.WdSaveOptions.wdDoNotSaveChanges

    This will suppress Word's prompt.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, July 16, 2015 3:33 PM
    Moderator
  • I think the only way you are going to manage that, is by cancelling the event and implementing your own version:

    //cancel the event
    
    Cancel = true;
    
    //if the doc is saved, default behaviour is to close
    if (!doc.saved) {
    	DialogResult userResponse = MessageBox.Show("Do you want to save changes?", "Close Document", MessageBoxButtons.YesNo);
    	if (userResponse == DialogResult.Yes) {
    		doc.save();
    	}
    }
    
    doc.close();



    • Edited by Nick Dewitt Thursday, July 16, 2015 3:51 PM used a c# msgbox
    Thursday, July 16, 2015 3:47 PM
  • Thanks for the answers so far.

    how is it possible to suppress this Dialog from Word? Because if i would set Cancel = true; in my BeforeClose function it will just suppress the dialog if Word will show the dialog after my function? But sometimes Word prompts the dialog before my function is executed (E.G. word is called by another program).

    Greetings
    Friday, July 17, 2015 2:00 PM
  • Hi SRH

    I'm not clear on your scenario. What kind of program is responsible for monitoring the DocumentBeforeClose event? A Windows Form? An Add-in? VBA?

    If the event handler is monitoring, then calling the Close method on any document -whether done by the user or another program - should trigger it. If the event handler sets Cancel = true, that means it cancels Word's default behavior, which includes displaying the message as well as closing the document. (The event's Cancel parameter is NOT the same as the cancel button of the message.)

    If another program is also monitoring the DocumentBeforeClose event there's not a blessed thing you can do to alter that behavior (nor can it alter your event handler).


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 17, 2015 4:10 PM
    Moderator
  • That's it. Thanks for the hint.

    the program(Software for Patent Laywers) which starts word loads a hidden plugin or kind of if thats possible?
    And this plugin displays the "Do you want to save changes?" dialog and suppress the word default dialog by setting Doc.Saved = true;. Because of that i thought my BeforeClose function is executed after the Word default dialog.

    Now I need another solution. Is it possible to change the Execution order of the plugins?

    Greets


    Friday, July 17, 2015 5:24 PM
  • Hi SRH

    <<Is it possible to change the Execution order of the plugins?>>

    No, it's not. And if you think about it, that makes a certain amount of sense since each would probably be vying for primacy...

    "Hidden" add-ins aren't really possible, but Add-ins that don't appear in the list available in the Com Add-ins dialog box are. Add-ins can be installed on a pro-user basis (CurrentUser hive in the Registry) or for the entire machine (LocalMachine hive in the Registry). Only the former show up in the Com Add-ins dialog box and can be turned off/on by the user.

    This makes it difficult to manage and is the reason Microsoft tried to do away with installing to the HKLM. But the cries of outrage were many and loud...

    I'm not an expert in Add-in loading/unloading and I don't think I have a machine-level add-in installed I can test on. Ideally would be if you could force unload/load the add-in through the COMAddin object of the Word.Application, but I'm not sure whether that has the same restrictions as the dialog box. You might also want to see if the information about managing Add-ins from the UI helps: https://support.office.com/en-us/article/Enable-or-disable-add-ins-in-Office-programs-3e533d0f-a6cf-44b7-b6b4-3d9185b5e025?ui=en-US&rs=en-US&ad=US&fromAR=1


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, July 17, 2015 6:11 PM
    Moderator
  • Thanks a lot.

    I did a workaround. It's not the best solution but it works.

    [Closed]
    Tuesday, August 4, 2015 8:58 PM
  • Hi,

    could you please share your workaround so people else could learn from it ?

    Thanks

    Best Regards,

    Lan


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, August 5, 2015 2:33 AM
    Moderator
  • Hi,

    To my first question:
    Like the People said. You have to set Cancel = True and the Word Dialog wont be displayed.
    But other plugins might present a dialog before you set Cancel = True. Like in my case.

    Second question:
    My "Workaround" is no real trick to track other plugins. I just ignored the other Dialog and displayed my own where I can handle the input.

    Best Regards.
    • Marked as answer by L.HlModerator Thursday, August 6, 2015 2:24 AM
    Wednesday, August 5, 2015 10:31 PM