Results 1 to 11 of 11
  1. #1
    New Lounger
    Join Date
    Aug 2008
    Location
    Fort Worth, Texas, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    VBA how to detect cancelled save (Word 2000 +)

    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?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA how to detect cancelled save (Word 2000 +)

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

  3. #3
    New Lounger
    Join Date
    Aug 2008
    Location
    Fort Worth, Texas, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA how to detect cancelled save (Word 2000 +)

    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.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: VBA how to detect cancelled save (Word 2000 +)

    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).

  5. #5
    New Lounger
    Join Date
    Aug 2008
    Location
    Fort Worth, Texas, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA how to detect cancelled save (Word 2000 +)

    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.

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: VBA how to detect cancelled save (Word 2000 +)

    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

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: VBA how to detect cancelled save (Word 2000 +)

    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.

  8. #8
    New Lounger
    Join Date
    Aug 2008
    Location
    Fort Worth, Texas, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA how to detect cancelled save (Word 2000 +)

    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.

  9. #9
    New Lounger
    Join Date
    Aug 2008
    Location
    Fort Worth, Texas, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA how to detect cancelled save (Word 2000 +)

    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!

  10. #10
    3 Star Lounger
    Join Date
    Mar 2003
    Location
    Elkins Park, Pennsylvania, USA
    Posts
    325
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA how to detect cancelled save (Word 2000 +)

    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.
    <font face="Comic Sans MS">That's what you do in a herd; you look out for each other!</font face=comic> - Mike

  11. #11
    New Lounger
    Join Date
    Aug 2008
    Location
    Fort Worth, Texas, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VBA how to detect cancelled save (Word 2000 +)

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •