• VBA how to detect cancelled save (Word 2000 +)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » VBA how to detect cancelled save (Word 2000 +)

    Author
    Topic
    #454603

    In Microsoft Word VBA can I tell that a user has cancelled a save? For example, a user clicks to close a document. Word asks the user to save. The user cancels. How do I detect this cancel?

    Viewing 2 reply threads
    Author
    Replies
    • #1128887

      I don’t think so, at least not easily. If I’m wrong someone will hopefully correct me.

      • #1128889

        I am looking to find a list of Windows Application and Document events such as WindowActivate, WindowSelectionChange but the help doesn’t give it to me in list form. I was going to test these. Our problem is that someone on VBA code is setting a flag that a user is closing Word, and this flag is not reset when the user cancels this event.

        • #1128891

          You can use the Object Browser for this – press F2 in the Visual Basic Editor to activate it.

          Or see Application Object, Document Object and Window Object (the latter has no events).

          • #1128893

            I just did a “Public WithEvents WordApp As Word.Application” in the ThisDocument VBA Code area and they show up. I tried all of them to see if a Cancel was returned and I only get the Document Close() event firing. Wow. I guess there isn’t the granularity there that’s needed. There has to be though. I did try Window_Activate, but there is no guarantee that the application window will be activated following a Cancel.

    • #1128895

      Will the .Saved state of the document give you the information you need? I know it doesn’t tell you if the cancel button was clicked, but it does tell you if the document has been saved since it was last changed.

      StuartR

      • #1128904

        Thanks for the input. However this will not work as we take over the DocumentBeforeSave event to handle saving ourselves. The order is “DocumentBeforeClose” sets the IsClosing flag to true. The user cancels and IsClosing is set to true (it shouldn’t now be as the user canceled). Eventually Word’s automatic AutoSave method fires to call the DocumentBeforeSave event. DocumentBeforeSave thinks the document or application is closing, but it is not!

    • #1128899

      You want to detect the cancel so you can prevent the document from closing? You could try this event: DocumentBeforeClose Event [Word 2007 Developer Reference]and check for Doc.Saved=False.

      • #1128902

        We don’t need to prevent the document from closing. We only need to know if the user pressed canceled so we can reset the an “IsClosing” flag. If we don’t reset this other processes occur on autosave that we don’t want occurring.

        • #1128952

          I don’t know if you have this luxury, but one approach that comes to mind is to “hijack” the FileSave command.

          The code below will display the Save As… dialog box. If the user clicks “Cancel,” it displays a Message Box; otherwise, it saves the file.

          Sub FileSave()
          Dim mySaveDialog As Dialog, myAnswer As Integer
          Set mySaveDialog = Dialogs(wdDialogFileSaveAs)
          myAnswer = mySaveDialog.Show
          If myAnswer = 0 Then MsgBox “The file was not saved.”, vbInformation + vbOKOnly, “FileSaveAs Test”
          End Sub

          Note that, because this sub is named FileSave, it forces the Save As… dialog box to appear every time the user clicks the Save button or chooses File: Save (which seemed to be in keeping with your needs, since that’s the only time one would have the option of clicking on a Cancel button.) I guess one could name it FileSaveAs, in which case it would only appear when the user selected File: Save As…, but it wouldn’t run when the user is saving a file for the very first time, at least not without customizing the toolbars and menus.

          • #1129048

            We have thousands of documents that are used in Word 2000, XP (2002), 2003 and 2007 in a supporting framework of COM components, so this is not an option. Thanks for the input anyway.

    Viewing 2 reply threads
    Reply To: VBA how to detect cancelled save (Word 2000 +)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: