Disable Excel save option but allow macro save - vba

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.

Related

How to prevent any changes in code and be able at same time to look at it? (VBA)

Is there, by chance, any way to prevent from changing code and simultaneously be able to look at it?
The purpose is introductory, so that user could look at code without ability to do any changes.
Thank you in advance
Here is a tricky one. You can add the following three subscripts and it will make the file Read Only and also stop anyone from saving the workbook unless they use the SaveForReal Subscript/Macro.
Inside the ThisWorkbook VBA Object:
Private Sub Workbook_Open()
Application.DisplayAlerts = False
ActiveWorkbook.ChangeFileAccess Mode:=xlReadOnly
Application.DisplayAlerts = True
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ThisWorkbook.Saved = True
Cancel = True
End Sub
Inside a Module Object:
Private Sub SaveForReal()
Application.EnableEvents = False
ThisWorkbook.Save
Application.EnableEvents = True
End Sub
To save the workbook, you need to open the VBA editor and Run the SaveForReal Subscript, otherwise the Save Button and Save As button does nothing.
Edit: Added On Open Read Only Change.

Track copying excel file

Is it possible to track if anyone saving as an excel file using VBA?
Thank you :)
Yes it is possible.
Use the Workbook.BeforeSave Event which occurs before the workbook is saved.
Then write a log in whatever way you prefer. Eg log into a text file or in a worksheet of the workbook itself.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI Then
' do Stuff for SaveAs
Else
' do stuff for Save
End If
End Sub

Prevent user from performing Save, but not SaveAs, on a workbook

I have a workbook that I don't want anyone to perform a Save on, but they can perform a SaveAs. The problem I'm running into is that when they do a SaveAs (using a ActiveWorkbook.SaveAs statement rather than File > Save As), it is not using the standard SaveAsUI. So I can't do a If SaveAsUI = False Then method.
Here's what I had it place until I realized it doesn't do what I need.
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If Not SaveAsUI Then
If Application.UserName <> "Robby" Then
MsgBox "You can't save this workbook!"
Cancel = True
End If
End If
End Sub

How to disable 'save' option in Excel but 'save as' should be still working

I need to disable the save option in Excel but I still need the save as option to be working... So I know how to disable both of the option by this VBA:
Private Sub Workbook_BeforeSave(ByVal SaveUI As Boolean, Cancel As Boolean)
MsgBox "You can't save this workbook!"
Cancel = True
End Sub
But How is it possible to disable save but save us to be working still.
Thank you for helping me
Try:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI = False Then
MsgBox "You can't save this workbook!"
Cancel = True
End If
End Sub
EDIT:
To delete the code from ThisWorkbook Class Module (delete every thing) we can use this code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If SaveAsUI = False Then
MsgBox "You can't save this workbook!"
Cancel = True
Else
With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
.DeleteLines 1, .CountOfLines
End With
End If
End Sub
NOTE:
This will delete everything in ThisWorkbook Class Module not only Workbook_BeforeSave
We must allow Trust access to the VBA project object model (File > Options > click Trust Center, click Trust Center Settings, and then click Macro Settings).

VBA and properly handling Macro Security Level

Q: What is recommended way of notifing user about loss of functionality unless User change macro security settings?
What I do now:
I display warning on the first sheet user see after opening Workbook, with explanation why things WONT work unless proper settings are set.
And I hide it on start up. (Which wont happen unless settings are OK)
But its not perfect solution:
That message is just one time. (While user could send that sheet to somebody else with different settings...)
Hiding and showing those few rows is treaded by Excel as changing document. (So just opening and closing excel will generate Save changed warning!)
Ok, here is best answer I could think off
(And big thx to #brettdj for suggestion!)
Sub HideMacroSecWarning()
Status = ThisWorkbook.Saved
Help.Range("Warning").rows.hidden = True
ThisWorkbook.Saved = Status
End Sub
Sub ShowMacroSecWarning()
Status = ThisWorkbook.Saved
Help.Range("Warning").rows.hidden = False
ThisWorkbook.Saved = Status
End Sub
Those are for showing and hiding macros. Saved state is preserved so only user actions can trigger "unsaved changes" dialogbox.
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call ShowMacroSecWarning
End Sub
Private Sub Workbook_Open()
Call HideMacroSecWarning
End Sub
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call ShowMacroSecWarning
End Sub
Private Sub Workbook_AfterSave(ByVal Success As Boolean)
Call HideMacroSecWarning
End Sub
Those assure that no matter the state of user settings warning will ALWAYS be displayed in SAVED FILE. (So even if for user Exel do not display that warning, somebody else who will open it on different machine will see that warning.)