Application.EnableCancelKey=xlDisabled Not Working - Able To Cancel Save via Esc Key!

davidnash

New Member
Joined
Nov 20, 2012
Messages
15
I'm trying to use Application.EnableCancelKey = xlDisabled so that when I save the workbook via VBA within the Before Close sub of the workbook module the user is not able to abort the save.

In my (large) production workbook:
  • This fails more often than not and pressing the 'esc' key a few times will cause the save to be aborted and the error message "Run-time error 1004: Document not saved". The only button I am able to click is 'End' (or help) - 'Continue' and 'Debug' are greyed out.
  • Occasionally the code does work and disable the 'esc' key but the vast majority of times 'esc' functions with a few presses.


In a smaller test workbook I put together with the same code:
  • This has some dummy random data across c.10 sheets (so that workbook takes some time to save) and some blank sheets with the same names as those referred to in my VBA code
  • Though the code is identical, in this workbook the code works 90% of the time and while it is possible to make the document abort saving by pressing 'esc' a large amount of times, 'esc' has to be pressed much more often than in my production workbook and the code only fails c.10% of the time rather than around 80% of the time.


What is the problem?
I'm worried that my production workbook is corrupt.

Though of course there is the question that the code even fails in the new test workbook I built - perhaps the size of the workbook influences how easily the code fails?

Can anyone help and spot any relevant errors in my code, or offer an opinion on whether my workbook could be corrupt (strange if the new test one fails too sometimes though) and how I may fix this?

Or any other solutions?!


I've put the example workbook on Google Drive
- you should be able to just download it without even a Google account at https://docs.google.com/file/d/0B9P0...it?usp=sharing)
To replicate my issue:
  • The various passwords are 'admin'
  • Go to any sheet and make any change
  • Then press ctrl+F4 to close the workbook
  • My code will run a dialog asking you if you want to save
  • Say Yes
  • Press 'esc' key many many times while the status bar shows the save operation in progress
  • Around 10% of the time the save operation will be aborted and the error message above displayed
  • The code seems more likely to fail if upon 'esc' not working, the sheet saving successfully and thus exiting leaving Excel open with no other workbooks, you then open the sheet again - for some reason the first instance of opening the workbook (with a brand new Excel application process) seems more reliable


My code is also below:

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)


Dim ConfirmDesireSave
Dim PreventSavePwdAttempt1
Dim PreventSavePwdAttempt2
Dim PreventSavePwdAttempt3
Dim PreventSavePwd As String
PreventSavePwd = "admin"


    'Go to system closing screen
    Application.Goto (Sheets("Close System").Range("AM1"))
    ActiveWindow.ScrollColumn = 1
    ActiveWindow.ScrollRow = 1
    
    'Save file so that the security measures at the end of this code are held in place
    ConfirmDesireSave = MsgBox("Do you wish to save changes to the system?" & vbNewLine & vbNewLine & "If you select No, you will require the Prevent Saving Password" & vbNewLine & vbNewLine & "Save? Select cancel to abort system closing", vbQuestion + vbYesNoCancel, "Save System Changes?")
        If ConfirmDesireSave = vbCancel Then
            Cancel = True
            'Go to start screen
            Application.Goto (Sheets("Start").Range("Z1"))
            ActiveWindow.ScrollColumn = 1
            ActiveWindow.ScrollRow = 1
            'Prevent asset register closing and 'enable macros' sheet loading as system not closing
            End
        Else
            If ConfirmDesireSave = vbYes Then
                Application.EnableCancelKey = xlDisabled
                ActiveWorkbook.Save
            Else
                If ConfirmDesireSave = vbNo Then
                    PreventSavePwdAttempt1 = InputBox("To prevent saving changes to the system please enter the Prevent Saving Password" & vbNewLine & vbNewLine & "This is attempt 1 of 3", "Enter Password to Prevent System Saving Changes")
                        If PreventSavePwdAttempt1 = PreventSavePwd Then
                        Else
                            PreventSavePwdAttempt2 = InputBox("To prevent saving changes to the system please enter the Prevent Saving Password" & vbNewLine & vbNewLine & "This is attempt 2 of 3", "Enter Password to Prevent System Saving Changes")
                                If PreventSavePwdAttempt2 = PreventSavePwd Then
                                Else
                                    PreventSavePwdAttempt3 = InputBox("To prevent saving changes to the system please enter the Prevent Saving Password" & vbNewLine & vbNewLine & "This is attempt 3 of 3" & vbNewLine & vbNewLine & "If you get the password wrong this time the system will not close", "Enter Password to Prevent System Saving Changes")
                                        If PreventSavePwdAttempt3 = PreventSavePwd Then
                                        Else
                                            Cancel = True
                                            'Go to start screen
                                            Application.Goto (Sheets("Start").Range("Z1"))
                                            ActiveWindow.ScrollColumn = 1
                                            ActiveWindow.ScrollRow = 1
                                            'Prevent asset register closing and 'enable macros' sheet loading as system not closing
                                            End
                                        End If
                                End If
                        End If
                End If
            End If
       End If


    'Go to enable macros screen (for usability and in case Asset Register the active sheet)
    Application.Goto (Sheets("Enable Macros").Range("AM1"))
    ActiveWindow.ScrollColumn = 1
    ActiveWindow.ScrollRow = 1
    
    'Block access to Asset Register and remove from navigation lists
    Sheets("Asset Register").Visible = xlSheetVeryHidden
    'Remove log out buttons at start screen
    Sheets("Start").Shapes("Start_AssetRegisterLogOut").Visible = False
    
    'Re-enable escape key
    'Application.EnableCancelKey = xlInterupt
    
    'Mark workbook as saved to prevent system dialog requesting whether to save
    ThisWorkbook.Saved = True
    
End Sub
I would really appreciate any help or insight you can offer.

Many thanks,

David
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Can you use the ONKEY command to deactivate/activate the ESC key?
Code:
Application.OnKey "{ESC}", "" 'Deactivate ESC Key
Your code here
Application.OnKey "{ESC}"  'Activate ESC Key
 
Upvote 0
Can you use the ONKEY command to deactivate/activate the ESC key?
Code:
Application.OnKey "{ESC}", "" 'Deactivate ESC Key
Your code here
Application.OnKey "{ESC}"  'Activate ESC Key

Thank you so much for your suggestion! I've always had really great responses here, but you're the first person to help with this - thanks.

Unfortunately it didn't work - actually one press of escape manages to abort the save process with this method of disabling escape!

I really can't understand why such a simple command (either version) doesn't work. I wonder if when entering the Save process, one sort of leaves the Excel environment and enters more the Windows environment - where perhaps the prevent escape commands don't take effect.
- But this doesn't explain why Application.EnableCancelKey = xlDisabled prevents escape working for the first few presses, but not many presses!

Do you have any other ideas?

I've changed my code slightly (didn't seem to help), but the latest is below:

Thank you so much.


Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)


Dim ConfirmDesireSave
Dim PreventSavePwdAttempt1
Dim PreventSavePwdAttempt2
Dim PreventSavePwdAttempt3
Dim PreventSavePwd As String
PreventSavePwd = "admin"


    'Go to system closing screen
    Application.Goto (Sheets("Close System").Range("AM1"))
    ActiveWindow.ScrollColumn = 1
    ActiveWindow.ScrollRow = 1
    
    'Save file so that the security measures at the end of this code are held in place
    ConfirmDesireSave = MsgBox("Do you wish to save changes to the system?" & vbNewLine & vbNewLine & "If you select No, you will require the Prevent Saving Password" & vbNewLine & vbNewLine & "If you select No and you have saved since logging into the Asset Register, the Asset Register will be available to any user the next time they open the system" & vbNewLine & vbNewLine & "Save? Select cancel to abort system closing", vbQuestion + vbYesNoCancel, "Save System Changes?")
        If ConfirmDesireSave = vbCancel Then
            Cancel = True
            'Go to start screen
            Application.Goto (Sheets("Start").Range("Z1"))
            ActiveWindow.ScrollColumn = 1
            ActiveWindow.ScrollRow = 1
            'Prevent asset register closing and 'enable macros' sheet loading as system not closing
            Exit Sub
        Else
            If ConfirmDesireSave = vbYes Then
                'Go to enable macros screen (to warn user to enable macros next time system opened, and in case Asset Register the active sheet)
                Application.Goto (Sheets("Enable Macros").Range("AM1"))
                ActiveWindow.ScrollColumn = 1
                ActiveWindow.ScrollRow = 1
                
                'Block access to Asset Register and remove from navigation lists
                Sheets("Asset Register").Visible = xlSheetVeryHidden
                'Remove log out buttons at start screen
                Sheets("Start").Shapes("Start_AssetRegisterLogOut").Visible = False
                
                'Disable cancel key to prevent aborting save
                Application.EnableCancelKey = xlDisabled
                'Application.OnKey "{ESC}", ""
                
                'Save workbook
                ActiveWorkbook.Save
                                              
                'Re-enable escape key
                Application.EnableCancelKey = xlInterupt
                'Application.OnKey "{ESC}"
                
                'Mark workbook as saved to prevent system dialog requesting whether to save
                ThisWorkbook.Saved = True
                
            Else
                If ConfirmDesireSave = vbNo Then
                    PreventSavePwdAttempt1 = InputBox("To prevent saving changes to the system please enter the Prevent Saving Password" & vbNewLine & vbNewLine & "If you enter the correct password and have saved since logging into the Asset Register, the Asset Register will be available to any user the next time they open the system" & vbNewLine & vbNewLine & "This is attempt 1 of 3", "Enter Password to Prevent System Saving Changes")
                        If PreventSavePwdAttempt1 = PreventSavePwd Then
                        'Mark workbook as saved to prevent system dialog requesting whether to save
                        ThisWorkbook.Saved = True
                        Exit Sub
                        Else
                            PreventSavePwdAttempt2 = InputBox("To prevent saving changes to the system please enter the Prevent Saving Password" & vbNewLine & vbNewLine & "If you enter the correct password and have saved since logging into the Asset Register, the Asset Register will be available to any user the next time they open the system" & vbNewLine & vbNewLine & "This is attempt 2 of 3", "Enter Password to Prevent System Saving Changes")
                                If PreventSavePwdAttempt2 = PreventSavePwd Then
                                'Mark workbook as saved to prevent system dialog requesting whether to save
                                ThisWorkbook.Saved = True
                                Exit Sub
                                Else
                                    PreventSavePwdAttempt3 = InputBox("To prevent saving changes to the system please enter the Prevent Saving Password" & vbNewLine & vbNewLine & "If you enter the correct password and have saved since logging into the Asset Register, the Asset Register will be available to any user the next time they open the system" & vbNewLine & vbNewLine & "This is attempt 3 of 3" & vbNewLine & vbNewLine & "If you get the password wrong this time the system will not close", "Enter Password to Prevent System Saving Changes")
                                        If PreventSavePwdAttempt3 = PreventSavePwd Then
                                        'Mark workbook as saved to prevent system dialog requesting whether to save
                                        ThisWorkbook.Saved = True
                                        Exit Sub
                                        Else
                                            Cancel = True
                                            'Go to start screen
                                            Application.Goto (Sheets("Start").Range("Z1"))
                                            ActiveWindow.ScrollColumn = 1
                                            ActiveWindow.ScrollRow = 1
                                            'Prevent asset register closing and 'enable macros' sheet loading as system not closing
                                            Exit Sub
                                        End If
                                End If
                        End If
                End If
            End If
       End If
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,728
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top