How to disable the save as prompt? - excel

If IsWorkbookOpen("CONTRACT\CONTRACTLIST_Cement.xlsx") Then
x = 0
Else
Application.DisplayAlerts = False
ActiveWorkbook.Close savechanges:=True
Application.DisplayAlerts = True
End If
Hi, despite using the above code, the save as prompt still occasionally appears and affect the program. Does anyone know how to stop it completely? The problem is that after I click save as, it will alert me that it was still open.

Try below code
Its always good to explcilty refer the workbook rather than ActiveWorkbook
Sub test()
If IsWorkbookOpen("CONTRACT\CONTRACTLIST_Cement.xlsx") Then
x = 0
Else
Application.DisplayAlerts = False
ThisWorkbook.Save
ThisWorkbook.Close False
Application.DisplayAlerts = True
End If
End Sub

You can use Workbook_BeforeSave Event in the ThisWorkbook object to capture the user selecting SaveAs (or using the keyboard shortcut), which would result in the Save As prompt being displayed and the SaveAsUI being set to true. If SaveAsUI is true, this means the user is trying to save the file as something else, so you can then cancel the save As operation.
Open up the Visual Basic Window (Alt + F11) and put the following code in ThisWorkbook.
Disable Save As
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If (SaveAsUI = True) Then
MsgBox "Sorry. I can't have you saving this file as something else."
Cancel = True
End If
End Sub
You can delete the MsgBox line if you want; i put it there as an example if you wanted to notify the user that the function was disabled
To disable both the Save and Save As functionalities, you would remove the if statement and cancel the Save operation, regardless of if the Save as prompt is displayed.
Disable Save and Save As
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "Sorry. I can't have you saving this file at all."
Cancel = True
End Sub
If you just want to disable the Save operation, you would only need to look for the occurrence where the user is saving, but the SaveAsUI is not being displayed (i.e. user is simply saving the file).
Disable Save
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If (SaveAsUI = False) Then
MsgBox "Sorry. I can't have you saving any updates to this file."
Cancel = True
End If
End Sub
Finally, note that the user will still get a prompt to save if the user simply closes the file. The user won't be able to save and the file will close, but if you want the experience to be a bit cleaner, you'll need to make an additional change. When a user closes a file, Excel checks the ThisWorkbook.Saved variable to see if the file has been saved. If it is false, it will prompt the user to Save the file. To prevent this as well, we can set this boolean to true without saving, thus "tricking" Excel into thinking the file has been saved
Disable Save and Save As, including after User attempts to close file
Add the following code after your Workbook_BeforeSave code.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ActiveWorkbook.Saved = True
MsgBox "Click OK to continue closing this file. Changes will not be saved."
End Sub

Related

How to prevent saving Excel workbook unless a command button is clicked?

I have a command button to save an Excel workbook in a particular format.
I want that the users are unable to save the workbook unless they click that command button.
To prevent the use of traditional saving options I used the code below:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "You can't save this workbook!"
Cancel = True
End Sub
How do I prevent traditional saving options but allow saving if the command button is clicked?
You can have a boolean flag to indicate whether or not saving is allowed and only set it to true when you call the macro that contains your logic for saving the workbook.
Add the following code under ThisWorkbook:
Public AllowSave As Boolean
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not AllowSave Then
MsgBox "You can't save this workbook!"
Cancel = True
End If
End Sub
Public Sub CustomSave()
ThisWorkbook.AllowSave = True
' TODO: Add the custom save logic
ThisWorkbook.SaveAs '....
MsgBox "The workbook has been saved successfully.", , "Workbook saved"
ThisWorkbook.AllowSave = False
End Sub
Note that you'll need to assign the CustomSave macro to the button that will be initiating the custom save.
Demo:

Protect VBA Excel from saving by others

I am trying to protect my Excel VBA from saving by others who are also using my macro. I tried using the below code but it is not working. I want to prevent others from saving the VBA Excel before closing as well as while using Ctrl+S for saving.
Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not SaveAsUI Then
Cancel = True
MsgBox "You cannot save this workbook"
End If
End Sub
I'm using Excel 2019 and I cannot reproduce your issue. The code works as expected. If I press Save I get the message box and it does not save. If I close the workbook, it asks me if I want to save, if I press Save I again get the message box and it does not save.
But you can try to add a Workbook_BeforeClose event with a ThisWorkbook.Saved = True to make VBA believe the workbook was already saved. This prevents the message box when closing the workbook, that asks if you want to save or not.
Option Explicit
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Saved = True
End Sub
Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not SaveAsUI Then
Cancel = True
MsgBox "You cannot save this workbook"
End If
End Sub

How to disable SaveAs in an open workbook but NOT disable Save

I am unable to find any code that ONLY disables SaveAs functionality without disabling Save.
The code listed is very effective in disabling SAVE and SAVE AS, but I want users to be able to save data but NOT have the opportunity to SAVEAS so they cannot create version of the work book. (copying a file in file manager is not a problem in this case - just saveas is the problem.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim xName As String
xName = "CancelBeforeSave"
If Not Evaluate("=ISREF('" & xName & "'!A1)") Then
Sheets.Add(after:=Worksheets(Worksheets.Count)).Name = xName & ""
Sheets(xName & "").Move after:=Worksheets(Worksheets.Count)
'To edit macros disable by changing to True
'Need to open workbook first with macros disabled
Sheets(xName & "").Visible = False
Exit Sub
End If
'To edit macros disable by changing to False
'Need to open workbook first with macros disabled
Cancel = True
End Sub
I would expect the workbook when open to not allow saveas without affecting other open workbooks.
Alternately from the code I supplied if we can just re-enable the save funtionality would be acceptable as well.
Just test if SaveAsUI is true and cancel saving.
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
MsgBox "Save As is disabled", vbInformation
Cancel = True
End If
End Sub
In the documentation of the Workbook.BeforeSave event you can find:
SaveAsUI
True if the Save As dialog box will be displayed due to changes made that need to be saved in the workbook.
Note that this is not a security feature.
This prevents users from using the dialog box "Save As". But with some VBA code in any other Excel sheet you can easily trick this. Anything you do to try to disallow a real save as can be worked around. This will not be a security feature. It's just to prevent save as "by accident". Anyone who really wants to do a save as and knows how to trick it will still be able to do it.

Excel VBA Read Only supersedes BeforeSave

I have code that incorporates business logic for saving documents (Excel 365) to ensure proper naming convention, file locations etc etc as a Sub Workbook_BeforeSave
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = True ''Cancels the Save from the button push or Ctrl+S
Application.EnableEvents = False
'' code code code
Application.EnableEvents = True
End Sub
The problem is that if the file is opened as Read-Only (as most will be) Excel will prompt that "the file is Read-Only" (pic a) and go to the Save As screen in the File Ribbon (pic b). The Workbook_BeforeSave sub won't kick in until the SAVE button is pressed. It also won't move off this screen even after the sub has run.
Is there any way to either:
Get in front of the Read-Only prompt ... or
Write some code to move off the Save As screen?
MS Read-Only promt (pic a)
Save As Screen (pic b)
Huge thanks in advance!
This is not a perfect approach but try this.
This will cancel the save completely even after pressing the Save button, you can add your own save code.
Edit: Just realized that the code below does not stop the Read Only Alert, but when you click Save it cancels the Save and Closes the Save As Menu. However, when they use Ctrl+S the Send Keys {ESC} triggers a Ctrl+ESC which opens the Star Menu...
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Disable Read Only Alert
Application.DisplayAlerts = False
'Disable Events
Application.EnableEvents = False
'Cancel Save
Cancel = True
'Do whatever code you need in here
'
'Mark Workbook as Saved, allowing for the file to close without an alert even if not saved
ThisWorkbook.Saved = True
'Send Escape Key to leave Save As Menu
Application.SendKeys "{ESC}", 1
'Enable Events
Application.EnableEvents = True
'Enable Alerts
Application.DisplayAlerts = True
End Sub

How to enable events so Workbook_BeforeSave gets called

My Workbook_BeforeSave event is not called before saving
This is my code:
Option Explicit
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
a = MsgBox("Do you really want to save the workbook?", vbYesNo)
If a = vbNo Then Cancel = True
End Sub
This is probably normal, because events are probably not enabled.
Now I tried to put Application.Events = True like this:
Option Explicit
Application.Events = True
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
a = MsgBox("Do you really want to save the workbook?", vbYesNo)
If a = vbNo Then Cancel = True
End Sub
This doesn't change anything, Workbook_BeforeSave is still not called up on saving. But when I close the excel file, following error message is displayed :
The english translation is "Compilation error: Incorrect instruction outside of a procedure."
Apparently the Application.Events = True is not at the right place, but where should I put it ?
Hope these will help:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) must be inside ThisWorkbook in a VBA project.
Application.EnableEvents = True can not be inserted outside procedure or function.
Events are enabled by default. So, there must be somewhere inside vba project Events are getting disabled. This can be searched by :
Once you are inside VBA project, Press Ctrl+F to open the Find dialog box. Then search for application.enableevents in the current project. Press Find Next. See the image below.
You can use a little sub to change and view the Application.EnableEvents status (ON/OFF). Place the sub under any standard module. See the image below.

Resources