Disable Save As In A Batch Of Excel Files - vba

I've been researching how to disable "Save As" in Excel and it seems possible. I have a macro that loops through a bunch of Excel files making changes. In that macro I would like to disable "Save As" for all of the files. Is this possible?

Using application object. You just add this lines to the code that loops in every file or workbook so it disable "save as" for each one :
Application.CommandBars("Worksheet Menu Bar").Controls("File").Controls("Save As...").Enabled = False
Application.CommandBars("Worksheet Menu Bar").Controls("File").Controls("Save").Enabled = False
Tell me how it goes.

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "You can't save this workbook!"
Cancel = True
End Sub

Related

Disable Excel save option but allow macro save

I'm creating an excel file and I want to disable the 'save' and 'save as...' option.
I found a lot of solutions on the internet, like this one in VBA:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
MsgBox "You can't save this workbook!"
Cancel = True
End Sub
It prevents user from saving changes, but I can't save my changes and that's the problem because I need to do more changes in the VBA code.
Is there a way to save my macro changes ? Like an administrator mode etc... ?
Thank you for your future answers.
Use a global variable to override the Save disable:
Dim override as Boolean
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
if Not(override) then
MsgBox "You can't save this workbook!"
Cancel = True
end if
End Sub
Sub SaveMyChanges()
override = true
ActiveWorkbook.Save
override = false
End Sub
You can also save while in Design Mode in VBA.
Sorry for the necro, but this works also and is what I do.

Autofill Read only Excel 2010 xlsm SaveAs file name box

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim saveIt As Boolean
If ThisWorkbook.Name = "Test.xlsm" Then
Sheets("Sheet1").Select
If Not IsEmpty(A1.Value) Then
MsgBox "This workbook is 'read only' Please rename this workbook."
strName = "Please enter a new file name."
ThisPAth = Application.ActiveWorkbook.Path
ThisFile = Range("B1").Value
ActiveWorkbook.SaveAs Filename:=ThisPath & ThisFile & ".xlsm", FileFormat:=52, CreateBackup:=False
Else
MsgBox "Cancelled."
End If
End If
End Sub
I have a password protected workbook (Test.xlsm" that is strictly for data entry. When the user opens the workbook as read only, enters the data, and then exits the workbook/template, I want the SaveAs dialog box that automatically pops up to have the contents of A1 of Sheet1 to be the "Suggested" file name that is autofilled in the SaveAs box.
I thought if I snagged the BeforeSave function that I could declare this path/filename but alas, it does not work. The autofill box displays "Copy of Test.xlsm". I do not even think it sees the above code.
How can I accomplish autofilling this box with the desired name. Thanks.
------------Update------------------
Rewrote code to below but it still is not snagging the default dialog box on save. Maybe I am misunderstanding the Workbook_BeforeSave function. I thought it was automatically called whenever the file is getting saved. I never want the user to save the file with the name Test.xltm (I changed file to a template to see if that made a difference) but to suggest the user rename file to the value in B1 for standardization reasons. The code is not getting called automatically. I was able to get similar code to work if I call it by executing a macro from the quick access toolbar for example, but cannot seem to get it to execute automatically whenever the user selects "Close", "Save" or "Save As" from the "File" pull down menu.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Dim saveDialog As FileDialog
Debug.Print "Hello"
Set saveDialog = Application.FileDialog(msoFileDialogSaveAs)
If ThisWorkbook.Name = "Test.xltm" Then
Application.EnableEvents = False
Debug.Print "Save as"
Set saveDialog = Application.FileDialog(msoFileDialogSaveAs)
With saveDialog
.InitialFileName = "foo.xlsm"
.Show
End With
Application.EnableEvents = True
Else
Debug.Print "Cancel"
End If
End Sub
The template is password protected and opened as read only by user so SaveAs dialog box should always open upon exit/save/save as. Correct? And shouldn't the Workbook_BeforeSave always get called under this circumstance?
Example:
Sub saveDialogTest()
Dim saveDialog As FileDialog
Set saveDialog = Application.FileDialog(msoFileDialogSaveAs)
With saveDialog
.InitialFileName = "Foo.xlsx"
.Show
End With
End Sub
If you use the FileDialog like this, it will allow you to change the suggested file name.

Using a VBA Dialog Box when closing a SpreadSheet

I would like a dialog box to appear when someone attempts to close an important Excel sheet. Namely I would like it to ask "Have you logged all Changes?" giving them the opportunity to click "Yes" or "No". If they click "Yes" the sheet will close, but if they Click "No" it will ask them to "Please log all changes in the Change Notes section".
Any and all help greatly appreciated.
This is the code you are looking for. It asks the question, and if No is clicked, it cancels the closing of the workbook:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
ureply = MsgBox("Have you Saved All Changes?", vbYesNo)
If ureply = vbNo Then
Cancel = True
End If
End Sub
This code needs to go on 'ThisWorkbook' (Shown above the word 'Modules') on your VBA editor

How to disable save and protected sheet prompt in excel using VBA

I have an excel sheet which is protected, since the sheet is protected I don't want the user to save it, and I don't want the save the sheet prompt to appear when someone closes the workbook. Till now I have been using this:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = True
Application.DisplayAlerts = False
End Sub
Using this code, save feature is disabled but the prompt is still appearing
Similar problem: Since the workbook is protected whenever someone tries to change the cell content it displays an alert, I want to disable that prompt message as well.
Can someone help me to fix this
L42 has already answered part of your question.
I want to disable that prompt message as well.
Do this. While protecting the sheet, unckeck the option Select Locked Cells. Now that will take care of the keyboard input while the sheet is locked and protected.
As for mouse inputs i.e the prompt showing up when you double click on the cell, use this :)
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
End Sub

How to disable SaveAs in Ms word in vb.net

How can i disable Save as in MS word or in any other office using vb.net?
You need to use VBA Macro to disable Save As Option.
Code to disable in Excel is :
Private Sub Workbook_BeforeSave (ByVal SaveAsUI As Boolean, Cancel As Boolean)
'This macro disables the "Save As" Feature in Excel
'This means that a user will not be able to save this
'workbook(file) under a different name or in a different location
'
'This MUST be placed in "ThisWorkbook" and NOT in a Module.
'
If SaveAsUI = True Then Cancel = True
End Sub