简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM